Further Visualisation


Further Visualisation

matlab



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.

Visualising multiple plots

Sometimes it can be useful to visualise multiple plots at once…

Method 1 multiple sets of arguments to plot

x = 0:0.1:2*pi;
y = sin(x);
y2 = cos(x);

plot(x,y,'-b', x,y2,'--r');
legend('Sine','Cosine');

Method 2 multiple plot commands on same figure

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');

Method 3 using multiple figures

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');

Method 4 using subplots

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');

Method 5 on same figure but with different y-axes

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');
A short summary of the different methods

To visualise multiple datasets on the same figure:

  • Use multiple sets of arguments to plot
  • Use hold on and multiple plot commands
  • Use subplot
  • Use plotyy if different y-scales required

To visualise multiple datasets of different figures:

  • Use the figure command to create multiple figures

3D plots

Multivariate data is data that has more than 1 value per ‘individual’, e.g.

  • Patient data: age, blood pressure, cholesterol level

Visualising multivariate data

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');

Visualising surfaces

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');

Imaging data

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 image
  • imwrite writes the array to a file
  • saveas saves the current figure (gcf) to a file


return  link
Written by Tobias Whetton