Showing posts with label Plot. Show all posts
Showing posts with label Plot. Show all posts

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)]});

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

Saturday, 7 December 2013

How to load the Matlab figure and extract data from the figure?

% To load figure

h = hgload('RectangularChipShape.fig')


get(h) % lists all the properties linked to this class

set(h) % lists all the properties that can be configured


Few examples:

set(gca,'fontsize',26);

hline = findobj(gcf, 'type', 'line');
hData = findobj(gcf, 'type', 'data');
set(hline,'LineWidth',4)
set(hline,'LineStyle',':')

idx = [4 5];
set(hline(idx),'Marker','*')


%To extract the data from the figure

axesObjs = get(h, 'Children'); %axes handles
dataObjs = get(axesObjs, 'Children'); %handles to low-level graphics objects in axes


objTypes = get(dataObjs{2});
xdata = get(dataObjs{2}, 'XData'); %data from low-level grahics objects
ydata = get(dataObjs{2}, 'YData');

%To extract data from Surf/Mesh plot
m = magic(10);

figure; h1 = mesh(m); % works for 'surf' also

h12 =gcf; % returns the figure number
 
h1Children = get(h12,'Children');
h1Chotas = get(h1Children,'Children'); % this is equal to h1
xD = get(h1Chotas,'xdata')
yD = get(h1Chotas,'ydata')
zD = get(h1Chotas,'zdata'


% Detailed explanation is available at:

http://www.mathworks.com/help/matlab/learn_matlab/understanding-handle-graphics-objects.html

Sunday, 10 November 2013

How to save Matlab graphs/plots with high resolution/quality (suitable for publications)?

Use the export-figure file from:

http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig

Examples can be found at:

https://sites.google.com/site/oliverwoodford/software/export_fig


Other settings:

% Colour adjustmentsset(gcf, 'Color', 'w'); %background outside the plot
set(gca, 'Color', 'w'); %background of the plot

% Set figure format - Size
% set(gcf,'Position',[xpos ypos scale*width scale*height])
set(gcf, 'Position', [100 100 150 150])
%or,
scrsz = get(0,'ScreenSize'); %returns the screen size
set(gcf, 'Position', [0 0 0.85*scrsz(3) 1*scrsz(4)]);

% Saving figure using export_fig
figName = [fName(1:end-4) '.png'];

saveas(gcf, figName);
export_fig(figName);

% Example:

function [] = SaveAndExportFigure(figName)
set(gcf, 'Color', 'w'); %background outside the plot
set(gca, 'Color', 'w'); %background of the plot
scrsz = get(0,'ScreenSize');
set(gcf, 'Position', [0 0 0.85*scrsz(3) 1*scrsz(4)]);
saveas(gcf, figName);
export_fig(figName);