008 MATLAB Graphics
008 MATLAB Graphics
GRAPHICS
2-D
FIGURE WINDOWS
MATLAB directs graphics output to a
window called figure that is separate
from the command window. The
figure function creates figure
windows.
Example: >>figure
2-D Plotting
The plot function is used to produce two-
dimensional curves, using x- and y-data
matrices specified by the user.
plot(xdata,ydata,’clm’)
>> y1 = 4*x – 2;
>> y2 = x + 2;
>> plot(x,y1,x,y2)
Adding a Grid
GRID ON creates a
grid on the current
figure
1. Graph y = 2cos3x
2. Graph the exponential function, logarithmic function,
inverse trigonometric function, hyperbolic function with
appropriate domain.
Adding Additional Plots to a Figure
By default, plot deletes existing lines and resets all axis
properties when a new line is drawn.
Example
Type the following commands in the command
window.
>>x=0:pi/20:2*pi;
>>y=sin(x);
>>plot(x,y,’bs-’,’linewidth’,2)
>>hold on
>>y1=cos(x);
>>plot(x,y1,’r>:’,’linewidth’,2)
ADDING A TITLE TO A GRAPH
There are several ways to add title to a graph:
Example:
>> title(‘Graph of Sine and Cosine Functions’)
ADDING A LEGEND TO A GRAPH
There are two ways to add legend to a graph:
Example:
>>legend(‘ Sine Function ’,’ Cosine Function ’)
ADDING AXES LABELS TO A GRAPH
There are three ways to add labels to a graph:
1. Using the Label Options on the Insert Menu.
(i) Click on the Insert menu and choose label
option that corresponds to the axes you want
to label.
(ii) Enter the text of the label, or edit the text of
an existing label.
2. Using the Property Editor.
(i) Start plot editing mode.
(ii) Double click on the axes on the graph to
open the Property Editor.
(iii) Select the Labels panel and enter the text of
the label in the appropriate text entry box.
(iv) Click Apply.
ADDING AXES LABELS TO A GRAPH
3. Using the Label Commands.
To add x, y and z axis labels to a graph use xlabel,
ylabel and zlabel functions.
Example:
>>xlabel(‘ x-axis’,’FontSize’,16)
ADDING TEXT ANNOTATIONS TO A
GRAPH
Examples:
1. >> subplot(2,2,1) 2. >> subplot(2,3,1)
>> subplot(2,2,2) >> subplot(2,3,2)
>> subplot(2,2,3) >> subplot(2,3,3)
>> subplot(2,2,4) >> subplot(2,3,4)
>> subplot(2,3,5)
>> subplot(2,3,6)
MULTIPLE PLOTS PER FIGURE
subplot(2,2,i) subplot(2,3,i)
where i = 1 to 4 where i = 1 to 6
Example
Type the following commands
in the command window.
>>x=0:pi/20:2*pi;
>>y=sin(x);
>>subplot(1,2,1)
>>plot(x,y,’bs-’,’linewidth’,2)
>>y1=cos(x);
>>subplot(1,2,2)
>>plot(x,y1,’r>:’,’linewidth’,2)
BASIC PLOTTING
COMMANDS
ezplot
ezplot is an easy to use function plotter for
algebraic and transcendental functions,
parametric equations, implicit and explicit
functions.
ezplot(f)
plots the expression f = f(x) over the
default domain -2 < x <
ezplot(f,[a,b])
plots f = f(x) over a < x < b
Examples:
>> subplot(2,3,1)
>> ezplot(‘cos(x)’)
>> subplot(2,3,2)
>> ezplot(‘cos(x)’,[0, pi])
>> subplot(2,3,3)
>> ezplot(‘1/y-log(y)+log(-1+y)+x-1’)
>> subplot(2,3,4)
>> ezplot(‘x^2+y^2-4’)
>> subplot(2,3,5)
>> ezplot(‘x^3+y^3-5*x*y+1/5’,[-3,3])
>> subplot(2,3,6)
>> ezplot(‘sin(t)’,’cos(t)’)
POLAR CURVES
Polar in polar coordinates can be created using the
polar(t,r,S) function, where t is the angle vector in radians, r
is the radius vector, and S is an optional character string
describing the color, marker symbol, and/or line style.
Example
>>t=linspace(0,2*pi);
>>r=sin(2*t).*cos(2*t);
>>polar(t,r)
>>title(‘Polar Plot’)
HISTOGRAM
Histogram illustrates the distribution of values in a vector.
hist(y) draws a 10-bin histogram for the data in vector y.
hist(y,n), where n is a scalar, draws a histogram with n bins.
hist(y,x), where x is a vector, draws a histogram using the
bins specified in x.
Example
>>x=-2.9:0.2:2.9; %specify the bins to
use
>>y=randn(5000,1); %generate 5000
random points
>>hist(y,x) %draw the
histogram
>>title(‘Histogram of Gaussian Data’)
PIE CHART
Standard pie charts can be created using the pie(a,b) function,
where a is a vector of values and b is an optional logical
vectors describing a slice or slices to be pulled out of the pie
chart. The pie3 function renders the pie chart with a 3-D
appearance.
Example
>>a=[0.5 1 1.6 1.2 0.8 2.1];
>>subplot(1,2,1)
>>pie(a,a==max(a)); %produces
chart a and pull out the biggest
slice.
>>subplot(1,2,2)
>>explode=[1 0 0 0 0 0 ];
>>pie(a,explode) % Which part is
pulled out?
BAR GRAPHS
Bar graphs display vector or matrix data. By default, a bar graph
represents each element in matrix. Bars in a 2-D graph, created
by bar function, are distributed along the x-axis with each
element in a column drawn at a different location. All elements
in a row are clustered around the same location on the x-axis.
Example
>> y =[5 2 1; 8 7 3; 9 8 6; 5 5 5;4 3 2];
>> subplot(1,2,1)
>> bar(y)
>> subplot(1,2,2)
>>bar3(y)
SPECIALIZED PLOT COMMANDS
• area • stem3
• pie3 • quiver
• rose • compass
• stairs • feather
MATLAB
GRAPHICS
3-D
ezsurf
ezsurf(f) creates a graph of f(x,y), where f is a
string that represents a mathematical function
of two variables, such as x and y.
Example:
>> subplot(1,2,1)
>> ezsurf('x^2+y^2')
>> subplot(1,2,2)
>> ezsurf('x^2-y^2')
ezmesh
ezmesh(f) creates a graph of f(x,y), where f is a
symbolic expression that represents a mathematical
function of two variables, such as x and y.
Example:
>> subplot(1,2,1)
>> ezmesh('x^2+y^2')
>> subplot(1,2,2)
>> ezmesh('x^2-y^2')
OTHER PLOTTING COMMANDS
• mesh
• contour
• contour3
• waterfall
• surf
• plot
FIGURE WINDOW TOOLS
ENJOY MATLAB GRAPHICS !