Vous êtes sur la page 1sur 6

# Basic Plotting Functions

On this page C reating a Plot Plotting Multiple Data Sets in One Graph Specifying Line Styles and C olors Plotting Lines and Markers Graphing Imaginary and C omplex Data Adding Plots to an Existing Graph Figure Windows Displaying Multiple Plots in One Figure C ontrolling the Axes Adding Axis Labels and Titles Saving Figures

Creating a Plot
The p l o tfunction has different forms, depending on the input arguments. If yis a vector, p l o t ( y )produces a piecewise linear graph of the elements of yversus the index of the elements of y . If you specify two vectors as arguments, p l o t ( x , y )produces a graph of yversus x . For example, these statements use the c o l o noperator to create a vector of xvalues ranging from 0 to 2, compute the sine of these values, and plot the result:

x=0 : p i / 1 0 0 : 2 * p i ; y=s i n ( x ) ; p l o t ( x , y )
Add axis labels and a title.

## x l a b e l ( ' x=0 : 2 \ p i ' ) y l a b e l ( ' S i n eo fx ' ) t i t l e ( ' P l o to ft h eS i n eF u n c t i o n ' , ' F o n t S i z e ' , 1 2 )

The characters \ p icreate the symbol and the F o n t S i z eproperty increases the size the text used for the title:

## Plotting Multiple Data Sets in One Graph

Multiple x -ypair arguments create multiple graphs with a single call to p l o t . MATLAB uses a different color for each line. For example, these statements plot three related functions of x :

## x=0 : p i / 1 0 0 : 2 * p i ; y=s i n ( x ) ; y 2=s i n ( x . 2 5 ) ; y 3=s i n ( x . 5 ) ; p l o t ( x , y , x , y 2 , x , y 3 )

The l e g e n dfunction provides an easy way to identify the individual lines:

## l e g e n d ( ' s i n ( x ) ' , ' s i n ( x . 2 5 ) ' , ' s i n ( x . 5 ) ' )

www.mathworks.co.uk/help/techdoc/learn_matlab/f3-27853.html

1/6

## Specifying Line Styles and Colors

It is possible to specify color, line styles, and markers (such as plus signs or circles) when you plot your data using the p l o tcommand:

p l o t ( x , y , ' c o l o r _ s t y l e _ m a r k e r ' ) c o l o r _ s t y l e _ m a r k e ris a string containing from one to four characters (enclosed in single quotes) constructed from a color, a line style, and a marker type. For
example,

p l o t ( x , y , ' r : + ' )
plots the data using a red-dotted line and places a +marker at each data point. The strings are composed of combinations of the following elements. Type C olor Values Meanings cyan magenta yellow red green blue white black solid dashed dotted dash-dot no line plus mark unfilled circle asterisk letter x filled square filled diamond filled upward triangle filled downward triangle filled right-pointing triangle filled left-pointing triangle filled pentagram filled hexagram no marker

' c ' ' m ' ' y ' ' r ' ' g ' ' b ' ' w ' ' k ' ' ' ' ' ' : ' ' . ' ' + ' ' o ' ' * ' ' x ' ' s ' ' d ' ' ^ ' ' v ' ' > ' ' < ' ' p ' ' h '

Line style

no character

## Plotting Lines and Markers

If you specify a marker type, but not a line style, MATLAB creates the graph using only markers, but no line. For example,

p l o t ( x , y , ' k s ' )
plots black squares at each data point, but does not connect the markers with a line. The statement

p l o t ( x , y , ' r : + ' )
plots a red-dotted line and places plus sign markers at each data point. Placing Markers at Every Tenth Data Point You can use fewer data points to plot the markers than you use to plot the lines. This example plots the data twice using a different number of points for the dotted line and marker plots:

## x 1=0 : p i / 1 0 0 : 2 * p i ; x 2=0 : p i / 1 0 : 2 * p i ; p l o t ( x 1 , s i n ( x 1 ) , ' r : ' , x 2 , s i n ( x 2 ) , ' r + ' )

www.mathworks.co.uk/help/techdoc/learn_matlab/f3-27853.html

2/6

## Graphing Imaginary and Complex Data

When you pass complex values as arguments to p l o t , MATLAB ignores the imaginary part, except when you pass a single complex argument. For this special case, the command is a shortcut for a graph of the real part versus the imaginary part. Therefore,

p l o t ( Z )
where Zis a complex vector or matrix, is equivalent to

p l o t ( r e a l ( Z ) , i m a g ( Z ) )
For example,

## t=0 : p i / 1 0 : 2 * p i ; p l o t ( e x p ( i * t ) , ' o ' ) a x i se q u a l

draws a 20-sided polygon with little circles at the vertices. The a x i se q u a lcommand makes the individual tick-mark increments on the x - and y -axes the same length, which makes this plot more circular in appearance.

## Adding Plots to an Existing Graph

The h o l dcommand enables you to add plots to an existing graph. When you type,

h o l do n
MATLAB does not replace the existing graph when you issue another plotting command. Instead, MATLAB combines the new graph with the current graph. For example, these statements first create a contour plot of the p e a k sfunction, then superimpose a p c o l o r(pseudocolor) plot of the same function:

## %O b t a i nd a t af r o me v a l u a t i n gp e a k sf u n c t i o n [ x , y , z ]=p e a k s ; %C r e a t ep s e u d o c o l o rp l o t p c o l o r ( x , y , z ) %R e m o v ee d g el i n e sas m o o t hc o l o r s s h a d i n gi n t e r p %H o l dt h ec u r r e n tg r a p h h o l do n %A d dt h ec o n t o u rg r a p ht ot h ep c o l o rg r a p h c o n t o u r ( x , y , z , 2 0 , ' k ' )

www.mathworks.co.uk/help/techdoc/learn_matlab/f3-27853.html

3/6

%R e t u r nt od e f a u l t h o l do f f
The h o l do ncommand combines the p c o l o rplot with the c o n t o u rplot in one figure.

Figure Windows
Graphing functions automatically open a new figure window if there are no figure windows already created. If there are multiple figure windows open, MATLAB uses the one that is designated as the "current figure" (the last figure used or clicked on). To make an existing figure window the current figure, you can click the mouse while the pointer is in that window or you can type,

f i g u r e ( n )
where nis the number in the figure title bar. To open a new figure window and make it the current figure, type

f i g u r e
Clearing the Figure for a New Plot When a figure already exists, most plotting commands clear the axes and use this figure to create the new plot. However, these commands do not reset figure properties, such as the background color or the colormap. If you have set any figure properties in the previous plot, you might want to use the c l fcommand with the r e s e toption,

c l fr e s e t
before creating your new plot to restore the figure's properties to their defaults.

## Displaying Multiple Plots in One Figure

The s u b p l o tcommand enables you to display multiple plots in the same window or print them on the same piece of paper. Typing

s u b p l o t ( m , n , p )
th subplot for the current plot. The plots are numbered along the first row of the partitions the figure window into an m -by-nmatrix of small subplots and selects the p figure window, then the second row, and so on. For example, these statements plot data in four different subregions of the figure window:

t=0 : p i / 1 0 : 2 * p i ; [ X , Y , Z ]=c y l i n d e r ( 4 * c o s ( t ) ) ; s u b p l o t ( 2 , 2 , 1 ) ;m e s h ( X ) s u b p l o t ( 2 , 2 , 2 ) ;m e s h ( Y ) s u b p l o t ( 2 , 2 , 3 ) ;m e s h ( Z ) s u b p l o t ( 2 , 2 , 4 ) ;m e s h ( X , Y , Z )

www.mathworks.co.uk/help/techdoc/learn_matlab/f3-27853.html

4/6

## Controlling the Axes

The a x i scommand provides a number of options for setting the scaling, orientation, and aspect ratio of graphs. Automatic Axis Limits and Tick Marks By default, MATLAB finds the maxima and minima of the data and chooses the axis limits to span this range. MATLAB selects the limits and axis tick mark values to produce a graph that clearly displays the data. However, you can set your own limits using the a x i sor x l i m ,y l i m , and z l i mfunctions. Note C hanging the limits of one axis can cause other limits to change to better represent the data. To disable automatic limit setting, enter the a x i sm a n u a l command. Setting Axis Limits The a x i scommand enables you to specify your own limits:

a x i s ( [ x m i nx m a xy m i ny m a x ] )
or for three-dimensional graphs,

a x i s ( [ x m i nx m a xy m i ny m a xz m i nz m a x ] )
Use the command

a x i sa u t o
to enable automatic limit selection again. Setting the Axis Aspect Ratio The a x i scommand also enables you to specify a number of predefined modes. For example,

a x i ss q u a r e
makes the x -axis and y -axis the same length.

a x i se q u a l
makes the individual tick mark increments on the x -axes and y -axes the same length. This means

p l o t ( e x p ( i * [ 0 : p i / 1 0 : 2 * p i ] ) )
followed by either a x i ss q u a r eor a x i se q u a lturns the oval into a proper circle:

a x i sa u t on o r m a l
returns the axis scaling to its default automatic mode. Setting Axis Visibility You can use the a x i scommand to make the axis visible or invisible.

a x i so n
makes the axes visible. This is the default.

a x i so f f
makes the axes invisible. Setting Grid Lines The g r i dcommand toggles grid lines on and off. The statement

g r i do n
turns the grid lines on, and

g r i do f f
turns them back off again.

www.mathworks.co.uk/help/techdoc/learn_matlab/f3-27853.html

5/6

## Adding Axis Labels and Titles

The x l a b e l ,y l a b e l , and z l a b e lcommands add x -, y -, and z-axis labels. The t i t l ecommand adds a title at the top of the figure and the t e x tfunction inserts text anywhere in the figure. You can produce mathematical symbols using LaTeX notation in the text string, as the following example illustrates:

t=p i : p i / 1 0 0 : p i ; y=s i n ( t ) ; p l o t ( t , y ) a x i s ( [ p ip i11 ] ) x l a b e l ( ' \ p i\ l e q{ \ i t t }\ l e q\ p i ' ) y l a b e l ( ' s i n ( t ) ' ) t i t l e ( ' G r a p ho ft h es i n ef u n c t i o n ' ) t e x t ( 0 . 5 , 1 / 3 , ' { \ i t N o t et h eo d ds y m m e t r y . } ' )

The location of the text string is defined in axes units (that is, the same units as the data). The a n n o t a t i o nfunction enables you to place text in normalized figure units.

Saving Figures
Save a figure by selecting Save from the File menu. This writes the figure to a file, including property data, its menus, uicontrols, and all annotations (i.e., the entire window). If you have not saved the figure before, the Save As dialog displays. This dialog box provides options to save the figure as a FIG-file or export it to a graphics format. If you have previously saved the figure, using Save again saves the figure "silently," without displaying the Save As dialog. To save a figure using a standard graphics format for use with other applications, such as TIFF or JPG, select Save As (or Export Setup, if you want additional control) from the File menu. Note Whenever you specify a format for saving a figure, that file format is used again the next time you save that figure or a new one. If you do not want to save in the previously used format, use Save As and be sure to set the Save as type drop-down menu to the kind of file you want to write. You can also save from the command lineuse the s a v e a scommand, including any options to save the figure in a different format. The more restricted h g e x p o r t command, which saves figures to either bitmap or metafile files, depending on the rendering method in effect, is also available. Saving Workspace Data You can save the variables in your workspace by selecting Save Workspace As from the figure File menu. You can reload saved data using the Import Data item in the figure File menu. MATLAB supports a variety of data file formats, including MATLAB data files, which have a . m a textension. Generating MATLAB Code to Recreate a Figure You can generate MATLAB code that recreates a figure and the graph it contains by selecting Generate code from the figure File menu. This option is particularly useful if you have developed a graph using plotting tools and want to create a similar graph using the same or different data.

Site Help

Patents