The Wayback Machine - https://web.archive.org/web/20110829131920/http://www.mathworks.com:80/help/techdoc/creating_plots/f6-20079.html
Skip to Main Content Skip to Search
Product Documentation

Using High-Level Plotting Functions

Functions for Plotting Line Graphs

Many types of MATLAB functions are available for displaying vector data as line plots, as well as functions for annotating and printing these graphs. The following table summarizes the functions that produce basic line plots. These functions differ in the way they scale the plot's axes. Each accepts input in the form of vectors or matrices and automatically scales the axes to accommodate the data.

Function

Description

plot

Graph 2-D data with linear scales for both axes

plot3

Graph 3-D data with linear scales for both axes

loglog

Graph with logarithmic scales for both axes

semilogx

Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis

semilogy

Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis

plotyy

Graph with y-tick labels on the left and right side

To view a gallery of all the high level plot functions, also with links to their reference pages, see Types of MATLAB Plots.

Programmatic Plotting

The process of constructing a basic graph to meet your presentation graphics requirements is outlined in the following table. The table shows seven typical steps and some example code for each.

If you are performing analysis only, you may want to view various graphs just to explore your data. In this case, steps 1 and 3 may be all you need. If you are creating presentation graphics, you may want to fine-tune your graph by positioning it on the page, setting line styles and colors, adding annotations, and making other such improvements.

Step

Typical Code

  1. Prepare your data

x = 0:0.2:12;

y1 = besselj(1,x);

y2 = besselj(2,x);

y3 = besselj(3,x);

  1. Select a window and position a plot region within the window

hf = figure;

subplot(2,2,1)

  1. Call elementary plotting function

h = plot(x,y1,x,y2,x,y3);

  1. Select line and marker characteristics

set(h,'LineWidth',2,{'LineStyle'},{'--';':';'-.'})

set(h,{'Marker'},{'none';'o';'x' })

set(h,{'Color'},{'r';'g';'b'})

  1. Set axis limits, tick marks, and grid lines

axis([0 12 -0.5 1])

grid on

  1. Annotate the graph with axis labels, legend, and text

xlabel('Time')

ylabel('Amplitude')

legend(h,'First','Second','Third')

title('Bessel Functions')

[y,ix] = min(y1);

text(x(ix),y,'First Min \rightarrow',... 'HorizontalAlignment','right')

  1. Export graph

set(hf,'PaperPositionMode','auto')

print -depsc -tiff -r200 myplot

Creating Line Plots

The plot function has different forms depending on the input arguments. For example, if y is a vector, plot(y) produces a linear graph of the elements of y versus the index of the elements of y. If you specify two vectors as arguments, plot(x,y) produces a graph of y versus x.

For example, the following statements create a vector of values in the range [0, 2π] in increments of π/100 and then use this vector to evaluate the sine function over that range. MATLAB plots the vector on the x-axis and the value of the sine function on the y-axis.

t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
grid on % Turn on grid lines for this plot

Appropriate axis ranges and tick mark locations are automatically selected.

Image shows a graph of the sine function from 0 to 2 pi.

You can plot multiple graphs in one call to plot using x-y pairs. MATLAB automatically cycles through a predefined list of colors (determined by the axes ColorOrder property) to allow discrimination between sets of data. Plotting three curves as a function of t produces

y = sin(t);
y2 = sin(t-0.25); 
y3 = sin(t-0.5);
plot(t,y,t,y2,t,y3)

Image showing 3 lines on single graph

Specifying Line Style

You can assign different line styles to each data set by passing line style identifier strings to plot. For example,

t = 0:pi/100:2*pi;
y = sin(t);
y2 = sin(t-0.25); 
y3 = sin(t-0.5);
plot(t,y,'-',t,y2,'--',t,y3,':')

The graph shows three lines of different colors and lines styles representing the value of the sine function with a small phase shift between each line, as defined by y, y2, and y3. The lines are blue solid, green dashed, and red dotted.

Image showing 3 lines with different lines styles on single graph

Colors, Line Styles, and Markers

The basic plotting functions accepts character-string arguments that specify various line styles, marker symbols, and colors for each vector plotted. In the general form,

plot(x,y,'linestyle_marker_color')

linestyle_marker_color is a character string (delineated by single quotation marks) constructed from

For example,

plot(x,y,':squarey')

plots a yellow dotted line and places square markers at each data point. If you specify a marker type, but not a line style, only the marker is plotted.

The specification can consist of one or none of each specifier in any order. For example, the string

'go--'

defines a dashed line with circular markers, both colored green.

You can also specify the size of the marker and, for markers that are closed shapes, you can specify separately the colors of the edges and the face.

See the LineSpec discussion for more information.

Specifying the Color and Size of Lines

You can control a number of line style characteristics by specifying values for line properties:

For example, these statements,

x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10)

produce a graph with

Adding Plots to an Existing Graph

You can add plots to an existing graph using the hold command. When you set hold to on, MATLAB does not remove the existing graph; it adds the new data to the current graph, rescaling if the new data falls outside the range of the previous axis limits.

For example, these statements first create a semilogarithmic plot, then add a linear plot.

semilogx(1:100,'+')
hold all % hold plot and cycle line colors
plot(1:3:300,1:100,'--')
hold off
grid on % Turn on grid lines for this plot

The x-axis limits are rest to accommodate the new data, but the scaling from logarithmic to linear does not change.

Image shows result of mixing semilog and linear plots in one graph

Plotting Only the Data Points

To plot a marker at each data point without connecting the markers with lines, use a specification that does not contain a line style. For example, given two vectors,

x = 0:pi/15:4*pi;
y = -exp(2*cos(x));

calling plot with only a color and marker specifier

plot(x,y,'r+') 

plots a red plus sign at each data point.

Image shows graph of expression plotted with red plus signs at each data point

See LineSpec for a list of available line styles, markers, and colors.

Plotting Markers and Lines

To plot both markers and the lines that connect them, specify a line style and a marker type. For example, the following code plots the data as a red, solid line and then adds circular markers with black edges at each data point.

x = 0:pi/15:4*pi;
y = -exp(2*cos(x));
plot(x,y,'-r',x,y,'ok') 

Image shows graph with solid red line and transparent circular markers at each data point

Line Styles for Black and White Output

Line styles and markers enable you to discriminate different plots on the same graph when color is not available. For example, the following statements create a graph using a solid ('-*k') line with asterisk markers colored black and a dash-dot ('-.ok') line with circular markers colored black.

x = 0:pi/15:4*pi;
y1 = -exp(2*cos(x));
y2 = -exp(2*sin(x));
plot(x,y1,'-*k',x,y2,'-.ok')

Image shows graph using line styles and markers, but no colors.

Setting Default Line Styles

You can configure MATLAB defaults to use line styles instead of colors for multiline plots by setting a value for the axes LineStyleOrder property using a cell array of linespecs. For example, the command

set(0,'DefaultAxesLineStyleOrder',{'-o',':s','--+'})

defines three line styles and makes them the default for all plots.

To set the default line color to dark gray, use the statement

set(0,'DefaultAxesColorOrder',[0.4,0.4,0.4])

See ColorSpec for information on how to specify color as a three-element vector of RGB values.

Now the plot function uses the line styles and colors you have defined as defaults. For example, these statements create a multiline plot.

x  = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-pi/2);
y3 = sin(x-pi);
plot(x,y1,x,y2,x,y3)

Image shows default values used in graph

The default values persist until you quit MATLAB. To remove default values during your MATLAB session, use the reserved word remove.

set(0,'DefaultAxesLineStyleOrder','remove')
set(0,'DefaultAxesColorOrder','remove')

See Setting Default Property Values for more information.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS