Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
48 views

008 MATLAB Graphics

This document provides an overview of MATLAB graphics capabilities for 2D and 3D plotting. It describes how to create figure windows and use basic plotting commands like plot, subplot, and ezplot to generate lines, functions, and multiple plots within a figure. Additional commands covered include bar graphs, histograms, pie charts, mesh/surface plots, and tools for customizing plots with colors, labels, legends and other annotations. The document is a useful reference for the core MATLAB graphics and visualization functions.

Uploaded by

Eriane Garcia
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
48 views

008 MATLAB Graphics

This document provides an overview of MATLAB graphics capabilities for 2D and 3D plotting. It describes how to create figure windows and use basic plotting commands like plot, subplot, and ezplot to generate lines, functions, and multiple plots within a figure. Additional commands covered include bar graphs, histograms, pie charts, mesh/surface plots, and tools for customizing plots with colors, labels, legends and other annotations. The document is a useful reference for the core MATLAB graphics and visualization functions.

Uploaded by

Eriane Garcia
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

MATLAB

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’)

You can plot multiple lines at once, using


pairs of x- and y-data, or triples of x, y.
plot(x1,y1,’clm1’,x2,y2,’clm2’)
Examples:
>> x = 0:10 ;
>> y = 2*x + 3;
>> plot(x,y)

>> y1 = 4*x – 2;
>> y2 = x + 2;
>> plot(x,y1,x,y2)
Adding a Grid
GRID ON creates a
grid on the current
figure

GRID OFF turns off


the grid from the
current figure

GRID toggles the


grid state
Color, Line Style and Marker
The user can specify the color, line
style and marker of a graph. If not, a blue
solid line without marker symbols, is
produced. Some of them are:
y yellow . point -- dashed
m magenta x x-mark -. dashdot
c cyan * star : dotted
r red + plus ^ triangle (up)
g green o circle p pentagram
b blue s square h hexagram
w white d diamond
k black v triangle (down)
Graph a red sine wave
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y,’r’)
>> grid on
Try to graph the following using the different line styles,
markers and colors.

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.

HOLD ON holds the current plot

HOLD OFF releases hold on the current plot

HOLD toggles the hold state


>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y,’r’)
>> grid on
>> hold on
>> plot(x, exp(-x),’b:*’)
Controlling Viewing Area
ZOOM ON allows user to ZOOM using the figure tool bar
select viewing area
ZOOM OFF prevents
zooming operations

ZOOM toggles the zoom


state

LEFT mouse button zoom in


(x2)
RIGHT mouse button zoom
out (x ½)

Double-click LEFT zoom


out completely
GRAPH ANNOTATIONS

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:

1. Using the Title Option on


the Insert Menu.

(i) Click the Insert menu in


the Figure window menu bar
and choose Title.

(ii) Enter the text of the label


and click anywhere in the
figure background to close
the text entry box.
ADDING A TITLE TO A GRAPH
2. Using the Property Editor to
Add a Title.

(i) Double click on the axes on


the graph to open the
Property Editor.

(ii) Select the Style panel and


type in the text of your title in
the Title entry box.

(iii) Click Apply.


ADDING A TITLE TO A GRAPH

3. Using the Title Function.


To add a title to a graph at the MATLAB command prompt,
use the title function.

Example:
>> title(‘Graph of Sine and Cosine Functions’)
ADDING A LEGEND TO A GRAPH
There are two ways to add legend to a graph:

1. Using the Legend Option on the Insert Menu.


- Click on the Insert menu and choose Legend.

2. Using the Legend Function.


To add a legend to a graph at the MATLAB command
prompt, use the legend function.

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

1. Creating Text Annotations in Plot Editing Mode.


(i) Click on the Insert menu and choose the Text
option or click the text button in the figure
window toolbar.
(ii) Position the cursor where you want to add a
text annotation in the graph and click.
(iii) Enter a text.
(iv) Click anywhere in the figure background to
close the text entry box.
ADDING TEXT ANNOTATIONS TO A
GRAPH
2. Adding Text Annotations with the text or gtext
Command.
To create annotations using text function, the text
and its location must be specified using the x and y
coordinates.
Example:
a. >>str1(1) = {‘Sine Function:‘}
>>str1(2) = {‘y=sin(x)‘}
>>text(3,0.5,str1)

b. >>str2(1) = {‘Cosine Function: ‘}


>>str2(2) = {‘y1=cos(x)‘}
>>text(0.3,-0.6,str2)
ADDING TEXT ANNOTATIONS TO A GRAPH
SAVING FIGURES

You can save


figures with Save
or Save As
through the File
menu on the
Figure Window.
This will create
a .fig file.
Displaying Multiple Plots per
Figure
Format: subplot(m,n,i)
This function breaks the figure window into m-by-n
matrix of small subplots and selects the ith subplot
for the current plot.

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

The following are some other 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 !

You might also like