Skip to main content
infervour.com

Back to all posts

How to Plot A Simple Graph In MATLAB?

Published on
6 min read
How to Plot A Simple Graph In MATLAB? image

Best Graph Plotting Tools to Buy in October 2025

1 KOALA TOOLS | Isometric Graph Paper Notebook (1 Unit) | 7.5" x 9.75", 60 pp. - Kraft Cover Isometric Grid Drawing Pads - Suitable for Industrial, Architectural, Interior Design

KOALA TOOLS | Isometric Graph Paper Notebook (1 Unit) | 7.5" x 9.75", 60 pp. - Kraft Cover Isometric Grid Drawing Pads - Suitable for Industrial, Architectural, Interior Design

  • VISUAL GUIDES FOR ARCHITECTS CREATE STUNNING 3D RENDERINGS EFFORTLESSLY.

  • COMPACT AND TRAVEL-FRIENDLY: 60 DOUBLE-SIDED PAGES FIT ANY BAG.

  • DURABLE, TEAR-RESISTANT PAPER EXCELS FOR HIGH-PRESSURE DRAFTING TASKS.

BUY & SAVE
$8.97
KOALA TOOLS | Isometric Graph Paper Notebook (1 Unit) | 7.5" x 9.75", 60 pp. - Kraft Cover Isometric Grid Drawing Pads - Suitable for Industrial, Architectural, Interior Design
2 Essential MATLAB for Engineers and Scientists

Essential MATLAB for Engineers and Scientists

BUY & SAVE
$37.72
Essential MATLAB for Engineers and Scientists
3 Koala Tools - 40-Page Drawing Pad for 2-Point Perspective, Room Grid Sketch Pad with 3D Virtual Walls Design, Gridded Graph Paper for Interior Room Design, Industrial, Architectural 11 x 17 inches

Koala Tools - 40-Page Drawing Pad for 2-Point Perspective, Room Grid Sketch Pad with 3D Virtual Walls Design, Gridded Graph Paper for Interior Room Design, Industrial, Architectural 11 x 17 inches

  • DRAW STUNNING INTERIORS EFFORTLESSLY WITH OUR 1-POINT PERSPECTIVE GRID.

  • PERFECT FOR HOBBYISTS: CREATE COMICS AND PERSPECTIVE SCENES WITH EASE.

  • DURABLE, EASY-TEAR SKETCHPAD BUILT FOR HIGH-PRESSURE DRAFTING NEEDS.

BUY & SAVE
$16.95
Koala Tools - 40-Page Drawing Pad for 2-Point Perspective, Room Grid Sketch Pad with 3D Virtual Walls Design, Gridded Graph Paper for Interior Room Design, Industrial, Architectural 11 x 17 inches
4 Koala Tools - 40-Sheet Sketch Pad for 2-Point Perspective Drawing, Spiral Bound Gridded Graph Paper for Interior Room Design, Industrial, Architectural and 3D Design, 11 x 14 inches

Koala Tools - 40-Sheet Sketch Pad for 2-Point Perspective Drawing, Spiral Bound Gridded Graph Paper for Interior Room Design, Industrial, Architectural and 3D Design, 11 x 14 inches

  • SKETCH EFFORTLESSLY WITH VARIED PERSPECTIVE GRIDS FOR 3D DESIGNS.

  • CRAFT IMPECCABLE SCENES FOR COMICS, MANGA, AND INTERIOR DESIGNS.

  • DURABLE, LARGE SHEETS WITH MICRO-PERFORATIONS FOR EASY REMOVAL.

BUY & SAVE
$19.95
Koala Tools - 40-Sheet Sketch Pad for 2-Point Perspective Drawing, Spiral Bound Gridded Graph Paper for Interior Room Design, Industrial, Architectural and 3D Design, 11 x 14 inches
5 MATLAB for Engineering and the Life Sciences (Synthesis Lectures on Engineering, Science, and Technology)

MATLAB for Engineering and the Life Sciences (Synthesis Lectures on Engineering, Science, and Technology)

BUY & SAVE
$47.49
MATLAB for Engineering and the Life Sciences (Synthesis Lectures on Engineering, Science, and Technology)
+
ONE MORE?

To plot a simple graph in MATLAB, you can follow these steps:

  1. Open MATLAB and create a new script file.
  2. Define the x-values and corresponding y-values that you want to plot. For example, you can create two vectors x and y to represent the x and y coordinates of the graph points.
  3. Plot the graph using the plot() function. Specify the x-values as the first input and the y-values as the second input. For instance, use plot(x, y) to plot the graph.
  4. Customize the graph if desired. You can add a title to the graph using title(), label the x and y-axis with xlabel() and ylabel(), respectively. You can also add a grid with grid on to make it easier to interpret the graph.
  5. Display the legend if you have multiple graphs on the same plot. You can specify the legend entries as input arguments to the legend() function.
  6. Save the graph as an image file, if necessary. You can use the saveas() function and provide a filename and desired file format (e.g., PNG, JPEG) as arguments.

Once you have completed these steps, you can run the script and a new figure window will open with the plotted graph.

How to plot a surface plot in MATLAB?

To plot a surface plot in MATLAB, you can use the "surf" function. Here are the steps:

  1. Define the x, y, and z data. The x data represents the x-coordinates, the y data represents the y-coordinates, and the z data represents the corresponding values at each (x, y) point.
  2. Use the "meshgrid" function to create a grid of x and y values from their respective vectors. This will create 2D arrays for x and y.
  3. Use the "meshgrid" output arrays and the z data to create a mesh or grid of (x, y, z) values using the "surf" function. The "surf" function will generate a 3D surface plot.
  4. Customize the plot appearance by adding axis labels, a title, and adjusting the viewing angle if desired.

Here is an example code:

% Step 1: Define x, y, and z data x = linspace(-5, 5, 100); y = linspace(-5, 5, 100); [X, Y] = meshgrid(x, y); Z = sinc(sqrt(X.^2 + Y.^2)); % Example z data

% Step 2: Create the grid [X, Y] = meshgrid(x, y);

% Step 3: Create the surface plot surf(X, Y, Z);

% Step 4: Customize plot appearance xlabel('X'); ylabel('Y'); zlabel('Z'); title('Surface Plot'); view(3); % Adjust viewing angle (optional)

This code will create a surface plot of the "sinc" function in a grid spanned by the x and y values. Remember to modify the x, y, and z data according to your specific application or dataset.

How to add gridlines to a MATLAB plot?

To add gridlines to a MATLAB plot, you can use the grid function. Here's how you can do it:

  1. Generate some data and create a plot:

x = 1:10; y = x.^2; plot(x, y);

  1. Use the grid function to add gridlines:

grid on;

By default, this will add both horizontal and vertical gridlines to the plot. If you want to add only horizontal or only vertical gridlines, you can modify the grid function as follows:

  • To add only horizontal gridlines: grid on; grid minor;
  • To add only vertical gridlines: grid on; grid minor on;

How to plot error bars in MATLAB?

To plot error bars in MATLAB, you can use the errorbar function. The syntax for errorbar is as follows:

errorbar(x, y, y_error)

Where:

  • x is a vector of x-axis values
  • y is a vector of y-axis values
  • y_error is a vector of the same length as y, representing the magnitude of the errors

Here is an example to demonstrate how to use errorbar function:

x = 1:5; % x-axis values y = [3 5 2 7 4]; % y-axis values y_error = [0.4 0.9 0.3 0.6 0.5]; % error values

errorbar(x, y, y_error) xlabel('X') ylabel('Y') title('Error bars')

This code will plot a line graph with error bars representing the uncertainty in the y-axis values. The xlabel, ylabel, and title functions are used to add labels and a title to the plot.

How to label the x-axis in a MATLAB plot?

To label the x-axis in a MATLAB plot, you can use the xlabel function. Here is an example:

% Create some data x = 1:10; y = rand(1, 10);

% Plot the data plot(x, y)

% Label the x-axis xlabel('X-axis label')

In this example, the xlabel function is used to provide the label for the x-axis. You can replace 'X-axis label' with your desired label.

How to plot a 3D graph in MATLAB?

To plot a 3D graph in MATLAB, you can use the plot3 function or the surf function. Here are the steps for each method:

  1. Using plot3 function: a. Define your x, y, and z data as vectors. b. Call the plot3 function and pass your x, y, and z data as arguments. c. Customize your plot with optional arguments like color and marker type. For example: x = linspace(-5, 5, 100); y = linspace(-5, 5, 100); [X, Y] = meshgrid(x, y); % Convert x and y to a grid Z = sin(sqrt(X.^2 + Y.^2)); plot3(X(:), Y(:), Z(:), 'r-'); % Plot 3D graph xlabel('X'); ylabel('Y'); zlabel('Z');
  2. Using surf function: a. Define your x and y data as matrices using the meshgrid function. b. Define your z data as a corresponding matrix. c. Call the surf function and pass your x, y, and z data as arguments. d. Customize your plot with optional arguments like color and shading. For example: x = linspace(-5, 5, 100); y = linspace(-5, 5, 100); [X, Y] = meshgrid(x, y); % Convert x and y to a grid Z = sin(sqrt(X.^2 + Y.^2)); surf(X, Y, Z); % Plot 3D graph xlabel('X'); ylabel('Y'); zlabel('Z'); colormap(jet); % Set color map shading interp; % Smooth shading colorbar; % Show color bar

These are the basic steps to plot a 3D graph in MATLAB. You can further customize your plots by changing axes limits, adding titles, legends, etc., based on your requirements.