| Contents | Index |
| On this page… |
|---|
Functions for Plotting Line Graphs Colors, Line Styles, and Markers Specifying the Color and Size of Lines Adding Plots to an Existing Graph |
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 |
|---|---|
Graph 2-D data with linear scales for both axes | |
Graph 3-D data with linear scales for both axes | |
Graph with logarithmic scales for both axes | |
Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis | |
Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis | |
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.
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.
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.

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)

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.

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
A line style (e.g., dashed, dotted, etc.)
A marker type (e.g., x, *, o, etc.)
A predefined color specifier (c, m, y, k, r, g, b, w)
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.
You can control a number of line style characteristics by specifying values for line properties:
LineWidth — Width of the line in units of points
MarkerEdgeColor — Color of the marker or the edge color for filled markers (circle, square, diamond, pentagram, hexagram, and the four triangles)
MarkerFaceColor — Color of the face of filled markers
MarkerSize — Size of the marker in units of points
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
A red dashed line with square markers
A line width of two points
The edge of the marker colored black
The face of the marker colored green
The size of the marker set to 10 points

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.

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.

See LineSpec for a list of available line styles, markers, and colors.
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')

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

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)

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.
![]() | Setting Up Figures | Line Plots of Matrix Data | ![]() |

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 |