Written and maintained by CASRAI Editorial Board
Last updated
Short answer: to plot a graph in MATLAB, pass x and y vectors to plot(x,y). Add a third argument such as "b--o" to set line style, marker and color, label the axes, then save the figure with exportgraphics. This guide goes past the basic call to a complete figure workflow: labels, legend, several plots in one axes, a grid of plots, and export for a manuscript.
What MATLAB plotting is used for in research
Researchers use plot to draw 2-D line graphs of measured or simulated data: time series, calibration curves, model fits, signal traces. A plot is only useful in a paper if it is labelled and exported in a format the publisher accepts, so this page covers those steps as well as the call itself. For the wider role of the software, see what MATLAB is used for.
Open MATLAB
MATLAB is a MathWorks product. Obtain it, or its browser version MATLAB Online, only from MathWorks (mathworks.com) or through your institution’s MathWorks arrangement. Type the commands below in the Command Window or save them in a script file.
How to plot a graph in MATLAB, step by step
The same steps answer the variants “plot graph in MATLAB”, “plot graph MATLAB” and “how to plot graph in MATLAB”.
- Create the data.
x = 0:pi/10:2*pi;theny = sin(x); - Plot it.
plot(x,y)draws y against x. With a single vector,plot(Y)plots against index. Table data usesplot(tbl,xvar,yvar). - Style it. Add a line specification:
plot(x,y,"r--o")is a red dashed line with circle markers. Or use name-value pairs:plot([0 1],[2 3],LineWidth=2). - Label it.
xlabel("Phase (rad)"),ylabel("Amplitude")andtitle("Sine wave").xlabel(txt)labels the x-axis of the current axes. - Add a legend.
legend("sin(x)")labels series in order. Place it withlegend({"sin(x)"},"Location","southwest"). - Export it.
exportgraphics(gcf,"sine.pdf",ContentType="vector").
Plot graph in MATLAB with points (markers)
Markers draw a symbol at each data point. Use a marker character in the line specification ("o") or the Marker name-value property. A marker with no line style, such as plot(x,y,"o"), gives a point-only plot. Default marker is none.
Several plots in the same axes
plot(x,y1,"g",x,y2,"b--o",x,y3,"c*") draws three series in one call. To add a series later, use hold on: it retains plots in the current axes so that new plots do not delete existing ones. hold off restores the default, in which a new plot clears the existing one.
A grid of plots: tiledlayout and subplot
subplot(m,n,p) divides the figure into an m-by-n grid and puts the axes at position p. MathWorks describes tiledlayout layouts as more customizable and configurable than those created by subplot, for example through adjustable tile spacing. With tiledlayout(2,2), call nexttile before each plot. Pass the layout object to title, xlabel and ylabel to label the whole layout:
t = tiledlayout(2,2,TileSpacing="compact");
nexttile; plot(x,sin(x))
nexttile; plot(x,cos(x))
title(t,"Trigonometric functions")
xlabel(t,"Phase (rad)")
Reference: line specification characters
| Property | Values (as documented for plot) |
|---|---|
| Line style | "-" solid (default), "--" dashed, ":" dotted, "-." dash-dotted, "none" |
| Marker | o + * . x _ |, square, diamond, ^ v > <, pentagram, hexagram |
| Color | red r, green g, blue b, cyan c, magenta m, yellow y, black k, white w, or an RGB triplet such as [1 0 0] |
For colormaps on surface and image plots, see MATLAB colormap colors.
Worked example: a labelled, exportable figure
x = 0:pi/10:2*pi;
y1 = sin(x); y2 = sin(x-0.25); y3 = sin(x-0.5);
plot(x,y1,"g",x,y2,"b--o",x,y3,"c*")
xlabel("Phase (rad)")
ylabel("Amplitude")
title("Phase-shifted sine waves")
legend("sin(x)","sin(x-0.25)","sin(x-0.5)","Location","southwest")
exportgraphics(gcf,"phase-shift.pdf",ContentType="vector")
exportgraphics(gcf,"phase-shift.png",Resolution=300)
The plot call is MathWorks’ own sine example; the labels, legend and export lines extend it.
Output and how to read it
A figure window opens with the axes drawn to fit the data. Each series is a separate line; the legend maps line style, marker and color to the label you supplied. If you give no label, the legend names series data1, data2 and so on, which is rarely acceptable in a paper. Alternatively set labels while plotting with the DisplayName property, then call legend.
Export for publication
exportgraphics(obj,filename) chooses the format from the file extension. Documented formats: jpg/jpeg, png, tif/tiff, gif, svg (since R2025a), pdf, emf (Windows only), eps, and html (since R2026a).
- Vector output (
ContentType="vector") keeps lines sharp at any size. It is not supported for JPEG, TIFF, PNG, GIF or HTML. - Raster output uses
Resolutionin DPI; the default is 150.exportgraphics(ax,"Peppers300.jpg",Resolution=300)is the vendor’s 300 DPI example. Resolution has no effect on HTML or on"vector"content.
Check your target journal’s artwork instructions for the format and resolution it requires; those rules are the journal’s, not MATLAB’s.
Troubleshooting
- Only the last plot appears. By default a new plot clears the existing one. Use
hold onafter the first plot, or plot several series in oneplotcall. - Legend says data1, data2. No label was supplied. Pass labels to
legendor setDisplayName. - Tick labels overlap between tiles.
TileSpacing="none"removes all space between adjacent plot boxes, so tick labels can overlap neighbouring plots. Use"tight","compact"or the default"loose". - Vector export fails or is ignored for PNG or JPEG. Vector content is unsupported for those formats; export PDF or EPS instead.
- EMF export unavailable. EMF is Windows only.
Free alternatives
| Tool | Equivalent call | Source |
|---|---|---|
| GNU Octave | plot (x, y, fmt); the fmt string takes linestyle, marker and color, for example "or" for red circles |
Octave manual, Two-Dimensional Plots |
| Python Matplotlib | import matplotlib.pyplot as plt, then plt.plot(x, y) |
Matplotlib quick-start guide |
For the same task in R, see 3D plots in R. To smooth data before plotting, see MATLAB moving average.
Frequently asked questions
How do I plot a graph in MATLAB?
Define x and y vectors and call plot(x,y). Add xlabel, ylabel and title to label it.
How do I plot a graph in MATLAB with points?
Include a marker in the line specification, for example plot(x,y,"o") for points only or plot(x,y,"-o") for a line with markers.
How do I put several graphs on one plot?
Use one plot call with several x,y pairs, or call hold on before adding more.
How do I add a legend?
Call legend("label 1","label 2") after plotting, in the same order as the series. The default 2-D location is northeast.
Should I use subplot or tiledlayout?
Prefer tiledlayout: MathWorks describes its layouts as more customizable and configurable than subplot layouts, including adjustable tile spacing.
How do I save the graph for a paper?
Use exportgraphics with a vector format such as PDF or EPS, or a raster format with Resolution=300.
Sources
Checked 2026-11-07.
- MathWorks, plot
- MathWorks, hold
- MathWorks, legend
- MathWorks, xlabel
- MathWorks, subplot and tiledlayout
- MathWorks, exportgraphics
- GNU Octave, Two-Dimensional Plots
- Matplotlib, Quick start guide







