plot

Draws 2-D line plots of one or more data sets.

Category: Language Fundamentals > Data Types

Syntax

plot(Y)

Plots the values of Y against their index. For a matrix, each column becomes a line.

plot(X, Y)

Plots Y against X.

plot(X, Y, LineSpec)

Sets the color, marker and line style with a short string such as 'r--o'.

plot(___, Name, Value)

Sets line properties such as 'LineWidth' or 'Color'.

h = plot(___)

Also returns the line handles.

Description

Each call draws in the current axes, replacing what was there unless hold on is active. In a live script the figure appears right below the code that creates it, and hovering shows a crosshair that reads values off the plot.

Use title, xlabel, ylabel, legend and grid to decorate the plot, and exportgraphics to save it as PNG or SVG.

Input arguments

X, Y
Vectors or matrices of coordinates. Vectors must have the same length.
LineSpec
Optional string combining a color (r g b c m y k w), a line style (-, --, :, -.) and a marker (o + * . x s d).
'LineWidth'
Line width in points.

Output arguments

h
Array of line handles, one per plotted line, for use with get and set.

Example

Two curves

x = linspace(0, 2*pi, 200);
plot(x, sin(x), 'LineWidth', 2)
hold on
plot(x, cos(x), '--')
hold off
grid on
legend('sin', 'cos')

In the app, the figure is drawn below the code.

See also

hold title xlabel legend exportgraphics plot3