A4 Plotting

Plotting occurs within figure windows that Matlab will create for us when we give it the command ‘figure’. There are two main plot types that we will use, the 2d and 3d scatterplot, and the mesh plot. First, let’s make a couple figure windows, and then we can try them out:

figure

figure(10)

figure

figure(1)

Notice that each of these three figure windows is numbered. You should have figures 1, 2, and 10. If you don’t specify a figure number (as we did with figure 10), it will use the lowest number that isn’t already in use. Also, if you write figure(1) when that figure already exists, you are telling Matlab you want figure(1) to be the current figure in terms of any subsequent plotting commands.


Now let’s make some scatterplots:

dat=[1.1,2.75,-.12,-1.23,3.84];

figure(1)

plot(dat)

figure(2)

subplot(2,1,1)

plot(1:length(dat),dat,'o','MarkerEdgeColor','k','MarkerFaceColor','b','MarkerSize',5)

subplot(2,1,2)

x=linspace(-2,2,101);

plot(x,x.^2-1,'.')

where notice we’ve specified several plot symbols, and line types. We can also create a 3d scatterplot:

x=linspace(-2,2,101);

y=-10:10;

z=nan(21,101);

for i=y,

z(i+11,:)=-x.^2-abs(i); end

figure(2)

plot3(ones(21,1)*x,y'*ones(1,101),z,'k.')

view(87,12)


The 3d scatterplot puts punctate plot symbols, like ‘.’, into 3d Cartesian axes. We can also create a 3d plot from the same data that interpolates a surface between those punctate positions using mesh:

figure(3)

mesh(x,y,z); colormap('bone')

view(70,14)


Finally, we can add to any plot using ‘hold on’. Here we will combine the 3d scatterplot and mesh:

figure(3); hold on

plot3(ones(21,1)*x,y'*ones(1,101),z,'k.')