Showing posts with label legend. Show all posts
Showing posts with label legend. Show all posts
Thursday, 18 December 2014
Tuesday, 11 March 2014
How to put legends only to some of the lines in a Matlab Plot?
1. Use the 'plot' handles.
x1 = 1:10;
y1blue = 2 .* x1;
y2green = 2 .* y1;
y3red = y2 + 5;
h1 = plot(x1,y1blue ,'b');
hold on;
h2 = plot(y2green,'g');
h3 = plot(y3red,'r');
%Legend all the line plots
legend('Y1blue','Y2green', 'Y3red');
%Legend only Y1 and Y2
legend([h1 h2],{'Y1','Y2'});
2. Another method to control the 'legend content' is given HERE
3. How to use numbers in the legend?
legend({['PRN' num2str(1)],['PRN' num2str(2)]});
x1 = 1:10;
y1blue = 2 .* x1;
y2green = 2 .* y1;
y3red = y2 + 5;
h1 = plot(x1,y1blue ,'b');
hold on;
h2 = plot(y2green,'g');
h3 = plot(y3red,'r');
%Legend all the line plots
legend('Y1blue','Y2green', 'Y3red');
%Legend only Y1 and Y2
legend([h1 h2],{'Y1','Y2'});
2. Another method to control the 'legend content' is given HERE
3. How to use numbers in the legend?
legend({['PRN' num2str(1)],['PRN' num2str(2)]});
Thursday, 19 December 2013
How to set the size of the texts and axes indices in Matlab plot?
0.8. How to check the properties that are accessible to the user?
set(gcf) % this command lists all the properties
0.9. Setting default font name for axis and texts in figure
set(0,'defaultAxesFontName', 'arial')
set(0,'defaultTextFontName', 'arial')
1. To set the properties of ALL that have the property 'fontsize' in figure, use:
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
% findall returns all the children which have the property 'fontsize'.
% This command will set size of the labels, legends, title, and axis indices
2. To set only the text part (this excludes the numbers (indices))
set(findall(gcf,'Type','text'),'FontSize',14);
%How to change the font name
set(findall(gcf,'Type','text'),'FontName','Arial');
%or
set(findall(gcf,'Type','text'),'FontName','Times New Roman');
3. To set the line width of all the 'lines' in a plot
set(findall(gcf,'Type','line'),'linewidth',2)
3.1 To set the MarkerSize, MarkerEdgeColor, MarkerFaceColor in a plot
set(findall(gcf,'Type','line'),'MarkerSize',10)
set(findall(gcf,'Type','line'),'MarkerEdgeColor','r')
set(findall(gcf,'Type','line'),'MarkerFaceColor','b')
%Above commands work for subplots as well
4. To control only the axes indices and legend when we have subplots
set(findall(gcf,'type','axes'),'fontsize',20);
%or
axes('FontSize',12)
% How to reduce the space between the subplots?
Explanation is given in this link.
Example is given below:
h2 = subplot(312), plot(freqBins,acfDelay,'-o')
p = get(h2, 'pos'); % returns [left, bottom, width, height] = [Pos(x,y), size(x,y)]
ylim([-30 40]);
set(gca,'ytick',[-30:15:30])
box off;
ylabel('Delay (m)');
set(gca,'xticklabel','')
grid on
p(2) = p(2) + 0.05;
set(h2, 'pos', p);
5. To apply these common settings to multiple figures:
When there are multiple figures, instead of having these commands for every plot, after generating all the plots use 'for loop' to apply these common settings to all the figures.
Use 'figure(n)' to select the figure to which the settings should be applied. This reduces the code size significantly and allows to easily modify all the figures at one place.
Example:
%% Beautifying figures
%To set the default font for texts and axes in figures.
set(0,'defaultAxesFontName', 'arial')
set(0,'defaultTextFontName', 'arial')
for figNum = 1:1:2
figure(figNum);
%To set the axes indices font
set(findall(gcf,'type','axes'),'fontsize',12);
%To set the line width
set(findall(gcf,'Type','line'),'linewidth',2)
%To set the fontsize of the text inside the plot (legend and other text)
set(findall(gcf,'Type','text'),'FontSize',12);
%Set the Font name
set(findall(gcf,'Type','text'),'FontName','arial');
%To adjust size of the figure
set(gcf,'Units','centimeters');
figSize = [20 15]; %width x height
set(gcf,'Position',[5 5 figSize]); % [position , size]
%Adding legends - split into two columns. 'columnlegend' is from
%FileExchange
columnlegend(2, legArray,'location','northEast','boxoff') ; % previous set commands remove this effect. %Hence, used after those
box off;
grid on;
%Saving the figure in meta file. This format allows us to edit the
%figure in MS-word.
saveas(gcf,'test1454523.emf','emf')
end
set(gcf) % this command lists all the properties
0.9. Setting default font name for axis and texts in figure
set(0,'defaultAxesFontName', 'arial')
set(0,'defaultTextFontName', 'arial')
1. To set the properties of ALL that have the property 'fontsize' in figure, use:
set(findall(gcf, '-property', 'FontSize'), 'FontSize', 12, 'fontWeight', 'normal')
% findall returns all the children which have the property 'fontsize'.
% This command will set size of the labels, legends, title, and axis indices
2. To set only the text part (this excludes the numbers (indices))
set(findall(gcf,'Type','text'),'FontSize',14);
%How to change the font name
set(findall(gcf,'Type','text'),'FontName','Arial');
%or
set(findall(gcf,'Type','text'),'FontName','Times New Roman');
3. To set the line width of all the 'lines' in a plot
set(findall(gcf,'Type','line'),'linewidth',2)
3.1 To set the MarkerSize, MarkerEdgeColor, MarkerFaceColor in a plot
set(findall(gcf,'Type','line'),'MarkerSize',10)
set(findall(gcf,'Type','line'),'MarkerEdgeColor','r')
set(findall(gcf,'Type','line'),'MarkerFaceColor','b')
4. To control only the axes indices and legend when we have subplots
set(findall(gcf,'type','axes'),'fontsize',20);
%or
axes('FontSize',12)
% How to reduce the space between the subplots?
Explanation is given in this link.
Example is given below:
h2 = subplot(312), plot(freqBins,acfDelay,'-o')
p = get(h2, 'pos'); % returns [left, bottom, width, height] = [Pos(x,y), size(x,y)]
ylim([-30 40]);
set(gca,'ytick',[-30:15:30])
box off;
ylabel('Delay (m)');
set(gca,'xticklabel','')
grid on
p(2) = p(2) + 0.05;
set(h2, 'pos', p);
5. To apply these common settings to multiple figures:
When there are multiple figures, instead of having these commands for every plot, after generating all the plots use 'for loop' to apply these common settings to all the figures.
Use 'figure(n)' to select the figure to which the settings should be applied. This reduces the code size significantly and allows to easily modify all the figures at one place.
Example:
%% Beautifying figures
%To set the default font for texts and axes in figures.
set(0,'defaultAxesFontName', 'arial')
set(0,'defaultTextFontName', 'arial')
for figNum = 1:1:2
figure(figNum);
%To set the axes indices font
set(findall(gcf,'type','axes'),'fontsize',12);
%To set the line width
set(findall(gcf,'Type','line'),'linewidth',2)
%To set the MarkerSize, MarkerEdgeColor, MarkerFaceColor in a plot
set(findall(gcf,'Type','line'),'MarkerSize',10)
set(findall(gcf,'Type','line'),'MarkerEdgeColor','r')
set(findall(gcf,'Type','line'),'MarkerFaceColor','b')
%To set the fontsize of the text inside the plot (legend and other text)
set(findall(gcf,'Type','text'),'FontSize',12);
%Set the Font name
set(findall(gcf,'Type','text'),'FontName','arial');
%To adjust size of the figure
set(gcf,'Units','centimeters');
figSize = [20 15]; %width x height
set(gcf,'Position',[5 5 figSize]); % [position , size]
%Adding legends - split into two columns. 'columnlegend' is from
%FileExchange
columnlegend(2, legArray,'location','northEast','boxoff') ; % previous set commands remove this effect. %Hence, used after those
box off;
grid on;
%Saving the figure in meta file. This format allows us to edit the
%figure in MS-word.
saveas(gcf,'test1454523.emf','emf')
end
Labels:
Beautify multiple figures at once,
figure fonts,
font name,
gcf,
legend,
line,
marker,
Plot,
properties,
saveas,
set,
size,
subplot
Subscribe to:
Posts (Atom)