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
.

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)

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

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.

To change the line color, line style, and marker type, add a
line specification input argument to the `x,y`

pair.
For example, `'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 the `LineSpec`

input
argument for `plot`

.

This example shows how to create a plot using a dashed line. Add the optional line specification, `'--'`

, to the `x,y`

pair.

```
x = linspace(0,2*pi,100);
y = sin(x);
figure
plot(x,y,'--')
```

This example shows how to plot two sine waves with different line styles by adding a line specification 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,':')

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

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

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

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,'*')
```

`contour`

| `linspace`

| `loglog`

| `plot`

| `plotyy`

| `scatter`

| `semilogx`

| `semilogy`

| `stairs`

| `stem`

- Add Title, Axis Labels, and Legend to Graph
- Change Axis Limits of Graph
- Specify Axis Tick Values and Labels

Was this topic helpful?