5. Matlab graphics

5.1 Plotting (x,y) data
5.2 Axis control
5.3 Plotting (x,y,z) data
5.4 Saving postscript graphs
5.5 Movies


5.1 Plotting (x,y) data

Matlab thinks in vectors when plotting (x,y) data.


A single data set with linear scales

>> x = 0:0.1:10;  y = sin(x);  plot(x,y)


If only one argument is given to plot, the data is equally spaced.

>> plot(cos(x))


A single data set with red *'s marking the data

>> plot(x,y,'r*')


Multiple data sets

>> x = 0:0.1:10;  plot(x,sin(x),x,cos(x))


Multiple data sets with different line styles

>> x = 0:0.1:10;  plot(x,sin(x),'--',x,cos(x),'.-')


Logarithmic scales are also available. See also loglog and semilogx

>> x = 0:0.1:10;  semilogy(x,exp(x))


5.2 Axis control


Rescaling is easily accomplished with axis. Simply define a vector with the minimum and maximums of x and y.

>> x = 0:0.1:10;  plot(x,sin(x),x,cos(x))
>> axis([0 2*pi -1 1])


Labeling is also easy. Enclose all labels in single quotes '

>> x = 0:0.1:10;  plot(x,sin(x));
>> title('sine'), xlabel('x axis'), ylabel('y axis');


Cut and paste the following commands, then use left mouse click in the figure window to place the text 'sin(x)'

>> x = 0:0.1:10;  plot(x,sin(x)); 
>> gtext('sin(x)')


5.3 Plotting (x,y,z) data


In the following example, a surface plot of the radius is created.

>> [X,Y] = meshgrid(-1:0.1:1,-1:0.1:1);
>> surf(sqrt(X.*X + Y.*Y))


A color plot without elevation can also be easily displayed.

>> pcolor(sqrt(X.*X + Y.*Y))


To smooth the coloring type

>> shading('interp')


And to remove the labeling

>> axis('off')


5.4 Saving postscript graphs


To save the current figure as a postscript file to print or import into another document, type

>> print -dps foo.ps


5.5 Movies



David Eyre
9/8/1998