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 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

No comments:

Post a Comment