REW

How Do You Graph A Variable In MATLAB?

Published Aug 29, 2025 4 min read
On this page

Plotting a variable in MATLAB is primarily done using the plot function, which creates a 2-D line plot of your data. The simplest use of plot requires you to provide vectors for the x- and y-coordinates. For more complex visualizations, you can use specialized functions like bar, scatter, stem, and subplot, or plot functions of multiple variables.

This guide covers plotting variables ranging from basic 2-D line plots to more advanced techniques.

Basic 2-D line plots

The most fundamental way to visualize a variable is by plotting it against another variable.

1. Create dataFirst, you must define the variables you want to plot. For continuous functions, it's best to create a set of evenly spaced points.

% Create a vector for the x-axis
x = 0:pi/100:2*pi;
% Create a vector for the y-axis (y is a function of x)
y = sin(x);

Use code with caution.

2. Plot the dataUse the plot(x, y) function to generate the graph.

plot(x, y)

Use code with caution.

3. Add labels and a titleUse the xlabel, ylabel, and title functions to make your plot clear and readable.

xlabel('x')
ylabel('sin(x)')
title('Plot of the Sine Function')

Use code with caution.

Advanced plotting techniques

Plotting a single variable against its index

If you only provide one variable to the plot function, MATLAB plots the values of the variable against their index (1, 2, 3, ...). This is useful for visualizing time-series data or a series of measurements.

grades = [90, 85, 92, 78, 95, 88];
plot(grades)
xlabel('Student Index')
ylabel('Grade')
title('Student Grades')

Use code with caution.

Plotting multiple variables on the same axes

To display multiple variables on a single plot, you have two primary options:

Method 1: Call plot with multiple pairs of variablesYou can provide multiple (x, y) pairs in a single plot command.

x = linspace(-2*pi, 2*pi);
y1 = sin(x);
y2 = cos(x);
plot(x, y1, x, y2)
legend('sin(x)', 'cos(x)') % Add a legend

Use code with caution.

**Method 2: Use hold on and hold off**The hold on command tells MATLAB to retain the current plot when you issue a new plot command. hold off returns to the default behavior of overwriting the previous plot.

x = linspace(-2*pi, 2*pi);
y1 = sin(x);
y2 = cos(x);
plot(x, y1)
hold on
plot(x, y2)
hold off
legend('sin(x)', 'cos(x)')

Use code with caution.

Plotting multiple graphs in one figure

For showing multiple independent plots in the same figure window, use the subplot function. It divides the figure into a grid of subplots.

The syntax is subplot(m, n, p), where m is the number of rows, n is the number of columns, and p is the position of the current plot.

x = linspace(0, 2*pi);
y1 = sin(x);
y2 = cos(x);
subplot(2, 1, 1); % Top subplot
plot(x, y1);
title('Sine Wave');
subplot(2, 1, 2); % Bottom subplot
plot(x, y2);
title('Cosine Wave');

Use code with caution.

Customizing your plot

You can customize almost every aspect of your plot by adding extra arguments to the plot function or by setting plot properties after creation.

Line specifications

A third argument to the plot function, called LineSpec, lets you specify the line style, marker, and color.

x = 0:pi/10:2*pi;
y = sin(x);
plot(x, y, '--rs') % Red dashed line with square markers

Use code with caution.

Common LineSpec options include:

  • Colors:'r' (red), 'b' (blue), 'k' (black).
  • Line styles:'-' (solid), '--' (dashed), ':' (dotted).
  • Markers:'o' (circle), '*' (asterisk), 's' (square).

Plot properties

You can use Name,Value pairs to modify plot properties, such as line width and marker size.

x = linspace(0, 2*pi);
y = sin(x);
plot(x, y, 'LineWidth', 2, 'Color', [0.85 0.33 0.1]); % Custom line width and color

Use code with caution.

Other plot types

The plot function creates a line plot by default. MATLAB offers many other specialized functions for different visualization needs.

  • bar(x, y): Creates a vertical bar graph.
  • scatter(x, y): Creates a scatter plot with circles at each data point.
  • stem(x, y): Creates a stem plot, where a vertical line extends from the x-axis to each data point.
  • errorbar(x, y, err): Creates a line plot with vertical error bars.

Plotting in 3-D

To plot functions of two variables, you can create a 3-D mesh or surface plot.

1. Create a gridUse meshgrid to generate a 2-D grid of x- and y-coordinates.

[X, Y] = meshgrid(-2:0.2:2, -2:0.2:2);

Use code with caution.

2. Define the functionEvaluate your function at each point in the grid.

Z = X .* exp(-X.^2 - Y.^2);

Use code with caution.

3. Create the 3-D plotUse mesh(X, Y, Z) to create a wireframe mesh plot or surf(X, Y, Z) to create a shaded surface plot.

surf(X, Y, Z);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Surface Plot of Z = X*e^(-X^2 - Y^2)');

Use code with caution.

Enjoyed this article? Share it with a friend.