Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction Part Ii: Graphics in Matlab

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

INTRODUCTION PART II

Graphics in Matlab
Creating a Plot
 The plot function can take one or more inputs;
e.g. plot (x, y) or plot (y)
 To plot a sin/cos function, can use
x = 0:pi/100:2*pi; y = sin(x); plot(x,y)
 To add title and labels to the plot
xlabel('x = 0:2\pi'); ylabel('Sine of x');
title('Plot of the Sine Function','FontSize',12)
 Remember, >> help plot is always useful
Multiple Data Sets in One Graph
 Multiple x-y pair arguments create multiple
graphs with a single call to plot
 Example: x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25);
y3 = sin(x-.5); plot(x,y,x,y2,x,y3);
 Legend command helps identify plots
legend('sin(x)','sin(x-.25)','sin(x-.5)');
 >> help legend % more about legend uses
 >> help title >> help xlabel >>help ylabel
Specifying Line Styles and Colours
 It is possible to specify colour, line styles, and markers (such
as plus signs or circles) when you plot your data using the plot
command
plot (x, y, 'color_style_marker')
 color_style_marker can be of:
 Colour signs: 'c' 'r' 'y' 'g' 'b' 'w'
 Line style strings: '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot
 The marker types are '+', 'o', '*', and 'x', and the filled marker
types are 's' for square, 'd' for diamond, etc.
Imaginary and Complex Data
 If Z is a complex number, then plot (Z) is the same as
plot (real (Z), imag (Z)); for example
t = 0:pi/10:2*pi; plot (exp(i*t),'-o'); axis equal

 This draws a 20-sided polygon and axis equal makes x


and y increment at same length
 Can also add markers to the plot,
x1 = 0:pi/100:2*pi; x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),'r:',x2,sin(x2),'r+');
Add Plots to Existing Graph
 The >> hold on command enables adding
other plots on top of the existing one(s)
 For example, the following first creates a
contour plot of the peaks function, then
superimposes a pseudocolor plot of the same
function
[x,y,z] = peaks; pcolor(x,y,z); shading interp;
hold on; contour(x,y,z,20,'k'); hold off
 >> help
hold/peaks/pcolor/interp/contour/shading
Figure Windows
 Matlab automatically opens a figure window if
one does not exist; but you can open and name
as many as you want
 >> Figure (n) opens a new figure with n
representing the number in the title bar
 If you need to clear a figure from existing
plot(s), then use >> clf reset
Multiple Plots in One Figure
 The subplot command enables displaying multiple plots
in the same window or print them on the same piece of
paper
 >> subplot(m,n,i) partitions the figure window into an m-
by-n matrix of small subplots and selects the ith subplot
for the current plot
 Example: t = 0:pi/10:2*pi; [X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X); subplot(2,2,2); mesh(Y);
subplot(2,2,3); mesh(Z); subplot(2,2,4); mesh(X,Y,Z);
Controlling the Axis
 The axis command provides a number of options for
setting the scaling, orientation, and aspect ratio of
graphs
 For axis limits, >> axis([xmin xmax ymin ymax])
 Can make axis visible/invisible,
>> axis on >> axis off
 The grid command toggles grid lines on and off
>> grid on >> grid off
 Check >> help axis >> help grid
Axis Labels and Titles
 The xlabel, ylabel, and zlabel commands add x-, y-,
and z-axis labels
 The title command adds a title at the top of the figure
and the text function inserts text anywhere in the
figure
 Example: t = -pi:pi/100:pi; y = sin(t); plot(t,y);
axis([-pi pi -1 1]); xlabel('-\pi \leq {\itt} \leq \pi');
ylabel('sin(t)'); title('Graph of the sine function');
text(1,-1/3,'{\itNote the odd symmetry.}');
 >> help annotation >> help tex
Handle Graphics
 Handle Graphics refers to a system of graphics
objects that MATLAB uses to implement
graphing and visualization functions
 For example, the following statement creates a
figure with a white background colour and
without displaying the figure toolbar
>> figure('Color','white','Toolbar','none')
 The following statements create a graph and
return a handle to a lineseries object in h >>
x = 1:10; y = x.^3; h = plot(x,y);
Handle Graphics
 You can use the handle h to set the properties of the lineseries
object. For example, you can set its Colour property: >>
set(h,'Color','red')
 You can also specify properties when you call the plotting
function >> h = plot(x,y,'Color','red');
 When you query the lineseries properties, >>
get(h,'LineWidth')
MATLAB returns the answer: ans = 0.5000
 Use the handle to see what properties a particular object
contains >> get(h)
Saving Figures
 Can save either from File menu or by typing >>
saveas
 You can save the variables in your workspace using the
Save Workspace As item in the figure File menu. You can
reload saved data using the Import Data item in the figure
File menu
 You can generate MATLAB code that recreates a figure
and the graph it contains by selecting the Generate M-File
item from the figure File menu
Working on Figure Window

 >> help graphics


 Can do everything we have seen
today and discover more advanced
options…
Exercise 1
 1. Create a vector x that goes from 0 to π by
increments of 0.1 and create a variable y that
is the sin function of x.
2. Plot the values of y as a function of x with
grid lines in the background of the plot (see
help grid for how to use grid lines), label the
axes something appropriate and give the plot
at title.
 3. Using the set function, modify the plot’s line
type, line width, color and marker size.
Exercise 2
 Using the subplot function graph the
trigonometric functions sin, cos, tan and cot
from 0 to 2pi. The layout of the plots shall be as
shown in the figure.

Sin Cos

Cot Tan

You might also like