Documentation

Create 2-D Graph and Customize Lines

Create 2-D Line Graph

This example shows how to create a simple line graph. Use the linspace function to define x as a vector of 100 linearly spaced values between 0 and $2\pi$.

x = linspace(0,2*pi,100);

Define y as the sine function evaluated at the values in x.

y = sin(x);

Plot y versus the corresponding values in x.

figure
plot(x,y)

Create Graph in New Figure Window

This example shows how to create a graph in a new figure window, instead of plotting into the current figure.

Define x and y.

x = linspace(0,2*pi,25);
y = sin(x);

Create a stairstep plot of y versus x. Open a new figure window using the figure command. If you do not open a new figure window, then by default, MATLAB® clears existing graphs and plots into the current figure.

figure % new figure window
stairs(x,y)

Plot Multiple Lines

This example shows how to plot more than one line by passing multiple x,y pairs to the plot function.

Define y1 and y2 as sine waves with a phase shift.

x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = sin(x-pi/4);

Plot the lines.

figure
plot(x,y1,x,y2)

plot cycles through a predefined list of line colors.

Colors, Line Styles, and Markers

To change the line color, line style, and marker type, add a line specification string to the x,y pair. For example, adding the string, 'g:*', plots a green dotted line with star markers. You can omit one or more options from the line specification, such as 'g:' for a green dotted line with no markers. To change just the line style, specify only a line style option, such as '--' for a dashed line.

For more information, see LineSpec (Line Specification).

Specify Line Style

This example shows how to create a plot using a dashed line. Add the optional line specification string, '--', to the x,y pair.

x = linspace(0,2*pi,100);
y = sin(x);

figure
plot(x,y,'--')

Specify Different Line Styles for Multiple Lines

This example shows how to plot two sine waves with different line styles by adding a line specification string to each x,y pair.

Plot the first sine wave with a dashed line using '--'. Plot the second sine wave with a dotted line using ':'.

x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = sin(x-pi/4);

figure
plot(x,y1,'--',x,y2,':')

Specify Line Style and Color

This example shows how to specify the line styles and line colors for a plot.

Plot a sine wave with a green dashed line using '--g'. Plot a second sine wave with a red dotted line using ':r'. The elements of the line specification strings can appear in any order.

x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = sin(x-pi/4);

figure
plot(x,y1,'--g',x,y2,':r')

Specify Line Style, Color, and Markers

This example shows how to specify the line style, color, and markers for two sine waves. If you specify a marker type, then plot adds a marker to each data point.

Define x as 25 linearly spaced values between 0 and $2\pi$. Plot the first sine wave with a green dashed line and circle markers using '--go'. Plot the second sine wave with a red dotted line and star markers using ':r*'.

x = linspace(0,2*pi,25);
y1 = sin(x);
y2 = sin(x-pi/4);

figure
plot(x,y1,'--go',x,y2,':r*')

Plot Only Data Points

This example shows how to plot only the data points by omitting the line style option from the line specification string.

Define the data x and y. Plot the data and display a star marker at each data point.

x = linspace(0,2*pi,25);
y = sin(x);

figure
plot(x,y,'*')

See Also

| | | | | | | | | |

Related Examples

Was this topic helpful?