31 December 2009 View Comments

Matlab Quick Reference: Plot


Matlab figure title with two lines

 


clc
plot(1:10)
title({'Comparison of Results';'using Mean Squared Error (MSE)'})
set(gca, 'Units', 'Normalized')
P=get(gca,'Position');
% Shrink the bottom margin by half its height and
% enlarge the top margin by the same amount
% (since the axis is the same height before & after)
set(gca, 'Position', [P(1) P(2)/2 P(3) P(4)])
disp('This is how it looks after moving the axis')

doubleline_plot

 

Double axis graphs

In order to plot y1=f(x1) and y2=g(x2) with two Y-axes, copy the following lines:

 

line(x1,y1,'Color','r');
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
ax2 = axes( 'Position',get(ax1,'Position'),. . .
'XAxisLocation','top',. . .
'YAxisLocation','right',. . .
'Color','none',. . .
'XColor','k','YColor','k');
line(x2,y2,'Color','k','Parent',ax2);

 

Set Tick location and Labels

 

% Generate a random vector of earning that can range from 
% $0 - $2000. Each element of earnings correspond to the 
% earnings for a given month. earnings = 1000 + 1000*rand(1,12) - 1000*rand(1,12); 
% Generate a bar plot of the earnings with 12 bins 
% (1 bin per month) bar(earnings); 
% Set the XLim so that it ranges from .5 - 12.5. 
% The reason that it starts at .5 and ends at 12.5 is 
% because each bin is .5 data units wide. set(gca,'XLim',[.5 12.5]); 
% This automatically sets the 
% XLimMode to manual. 
% Set XTick so that only the integer values that 
% range from 0.5 - 12.5 are used. set(gca,'XTick',[1:12]) 
% This automatically sets 
% the XTickMode to manual. 
% Set the XTickLabels so that abbreviations for the 
% months are used
months = ['Jan'; 'Feb'; 'Mar'; 'Apr'; 'May'; 'Jun'; 'Jul'; 'Aug'; 'Sep'; 'Oct'; 'Nov'; 'Dec']; 
set(gca,'XTickLabels',months)

 

 

2 Variable Regression With Transparent Plan

n=20;
% make some random data
x = rand(n,1);
y = rand(n,1);
% with an arbitrary function
z = x + 2*y + 3 + randn(n,1)/3;
% do the regression
coef = [x,y,ones(n,1)]\z
% generate a patch
xy = [0 0;0 1;1 1;1 0;0 0];
% and get the predictions at those points
zpred=[xy,ones(5,1)]*coef;
% plot the data in 3d
plot3(x,y,z,'ko')
grid on
box on
rotate3d on
% plot a patch using patch
patch(xy(:,1),xy(:,2),zpred,'r')
alpha(0.2)

untitled.png2 Variable 2 Variable Regression With Transparent PlanRegression With Transparent Plan

 

RGB to Gray colors

 

clear;
x = rand(5);
subplot(2,1,1);
pcolor(x);
subplot(2,1,2);
h = pcolor(x);
C = get(h,'CData');
C_rgb_gray = cat(3,C,C,C);
set(h,'CData',C_rgb_gray);

RGB to Gray colors

 

Projection of mesh contour onto Y face

[x,y,z] = peaks;
surf(x,y,z);
hold on;
= contour(z,y,x);
rotate(h,[0 1 0],-90,[0 0 0]);
XLims = xlim;
XData = get(h,'XData');
for i = 1 : length(h)
sz = size( XData{i} );
set(h(i), 'XData', repmat(XLims(2),sz) );
end

Projection of mesh contour onto Y face

 

Full Screen figure

 

function maxwindow
%MAXWINDOW - Maximize figure window
%
%Syntax: maxwindow
screen=get(0,'screensize');
if screen(3)==800
set(gcf,'Units','normalized','Position',[0 0.0467 1.00 0.84])
elseif screen(3)==1024
set(gcf,'Units','normalized','Position',[0.00 0.032 1.00 0.92])
elseif screen(3)==1152
set(gcf,'Units','normalized','Position',[0.00 0.032 1.00 0.89])
elseif screen(3)==1280
set(gcf,'Units','normalized','Position',[0.00 0.032 1.00 0.895])
elseif screen(3)==1600
set(gcf,'Units','normalized','Position',[0.00 0.032 1.00 0.905])
end

 

Colormap changing

 

surf(peaks)
for i=1:size(colormap,1)
  M = colormap;
  colormap(M([end 1:end-1],:))
  colorbar
  drawnow
end
figure('Units','pixels','Position',[100 100 200 300])

 

Surface fitting

You have a non-uniform collection of measured points (from total station, differential gps, etc.) and you want to plot an interpolated surface. Suppose your points are stored as X, Y, Z. First you build a uniform grid based on the extremities of the measured area:

[Xm, Ym] = meshgrid (Xmin:s:Xmax, Ymin:s:Ymax);

where s is the grid size. Then you interpolate the surface at all the grid points:

Zm = griddata (X, Y, Z, Xm, Ym);

Eventually you may plot the surface:

surf(Zi)

blog comments powered by Disqus