In the early chapters, we have touched on how the plot command can be used to generate simple visualisations and how we can then annotate these plots use functions such as title & xlabel.
Sometimes it can be useful to visualise multiple plots at once…
x = 0:0.1:2*pi;
y = sin(x);
y2 = cos(x);
plot(x,y,'-b', x,y2,'--r');
legend('Sine','Cosine');
x = 0:0.1:2*pi;
y = sin(x);
y2 = cos(x);
figure; hold on;
plot(x,y,'-b');
plot(x,y2,'--r');
title('Sine and cosine curves');
legend('Sine', 'Cosine');
x = 0:0.1:2*pi;
y = sin(x);
y2 = cos(x);
figure;
plot(x,y);
title('Sine curve');
figure;
plot(x,y2);
title('Cosine curve');
x = 0:0.1:2*pi;
y = sin(x);
y2 = cos(x);
subplot(2,1,1);
plot(x,y);
title('Sine curve');
subplot(2,1,2);
plot(x,y2);
title('Cosine curve');
figure;
[AX H1 H2] = plotyy(x,y1,x,y2);
title('Exponentially decaying oscillations'); xlabel('Time');
set(get(AX(1), 'Ylabel'), 'String', 'Low frequency oscillation');
set(get(AX(2), 'Ylabel'), 'String', 'High frequency oscillation');
To visualise multiple datasets on the same figure:
plothold on and multiple plot commandssubplotplotyy if different y-scales requiredTo visualise multiple datasets of different figures:
figure command to create multiple figuresMultivariate data is data that has more than 1 value per ‘individual’, e.g.
For data with 3 values per ‘individual’ we can use the plot3 function, e.g.
% the z-coordinate of the helix
t = 0:0.05:10*pi; % 5 times round
% x-coordinate of the helix
st = sin(t);
% y-coordinate of the helix
ct = cos(t);
% 3-D plot
figure;
plot3(st, ct, t, '.b') title('Helix');
xlabel('X'), ylabel('Y'), zlabel('Z');
Whereas plot3 is used for visualising multivariate data as lines/points etc., the mesh and surf commands can be used to visualise surfaces. e.g.
% create grid of x/y values
[X,Y] = meshgrid(-8:.5:8, -8:.5:8);
% define sinc function
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
% mesh plot
mesh(X,Y,Z)
title('Sinc function');
xlabel('X');
ylabel('Y');
zlabel('Z');
Matlab provides a number of commands for reading/writing/visualising images, e.g.
% read image
im = imread('fluoro.tif');
% display image
imshow(im);
% modify image
im(100:120,100:120) = 0;
% save image (saves 'im' array)
imwrite(im, 'fluoro_imwrite.tif');
% save image (saves figure as displayed)
saveas(gcf, 'fluoro_saveas.tif');
imread reads an image from a file into a 2D array.imshow displays the imageimwrite writes the array to a filesaveas saves the current figure (gcf) to a file