How to Plot 3D in MATLAB (2024)

  1. Use the mesh() Function to Create Surface Plots in MATLAB
  2. Use the scatter3() Function to Create a 3D Scatter Plot in MATLAB
  3. Use the contour3() Function to Create a 3D Contour Plot in MATLAB
  4. Use the plot3() Function to Create a 3D Plot of Point or Line in MATLAB
  5. Use the quiver3() Function to Create a 3D Quiver or Vector Plot in MATLAB
  6. Use the bar3() Function to Create a 3D Bar Graph in MATLAB
How to Plot 3D in MATLAB (1)

This tutorial will discuss creating 3d plots using the mesh(), scatter3(), contour3(), plot3(), quiver3(), and bar3() functions in MATLAB.

Use the mesh() Function to Create Surface Plots in MATLAB

Matlab’s built-in function mesh() creates the surface plots on a 3D plane. We can pass the matrix inside the mesh() function as a function that we want to plot in the 3D plane.

The mesh() function will plot the given matrix along the z-axis using the default value for the x-y coordinates. The x and y coordinates will be from the indices of the given matrix.

The given matrix will be plotted on the z-axis as heights on a three-dimensional plane.

For example, let’s create a matrix Z and plot it on the surface plot using the mesh() function in Matlab.

See the code below.

clc[x,y] = meshgrid(-18:1:18);r = sqrt(x.^2 + y.^2)/2;z = cos(r)./r;mesh(x,y,z)

Output:

How to Plot 3D in MATLAB (2)

Use the scatter3() Function to Create a 3D Scatter Plot in MATLAB

If we want to create a 3D scatter plot, we can use the scatter3() function. This function is the same as the scatter() function, though it plots the given data in a 3D plane.

We can give two or three input vectors to the scatter3() function.

In the case of three inputs, the first vector contains the x coordinates, the second contains the y coordinates, and the third contains the z coordinates.

In the case of two input vectors, the third coordinate z will be taken from the indices of the first two coordinates.

For example, let’s plot a scatter plot in a 3D place using the scatter3() function.

See the code below.

clcx = linspace(0,2*pi,100);y = sin(x) + rand(1,100);z = 1:100;scatter3(x,y,z)

Output:

How to Plot 3D in MATLAB (3)

Check this link for more details about the scatter() function.

Use the contour3() Function to Create a 3D Contour Plot in MATLAB

We can use Matlab’s built-in function contour3() to create a 3D contour plot. A contour plot is a plot of isolines with different colors according to values.

The color given to the line depends on its value. The colder color corresponds to low values, and the hotter color corresponds to high values.

For example, let’s plot a 3D contour plot of a sphere using the sphere() and the contour3() function. See the code below.

clc[x,y,z] = sphere(100);contour3(x,y,z);

Output:

How to Plot 3D in MATLAB (4)

Use the plot3() Function to Create a 3D Plot of Point or Line in MATLAB

We already know about the plot() function in Matlab, which is used to plot data on a 2D plane. The plot3() function is similar to the plot() function.

The only difference is that the plot3() function plots data on a 3D plane. We can plot data as a line, aka a continuous plot, and as points, aka a discrete plot.

We have to pass three coordinates, x, y, and z, to plot the data on a 3D plane. For example, let’s plot a helix in 3D using the plot3() function.

See the code below.

clct = 0:pi/50:10*pi;sinet = sin(t);cost = cos(t);plot3(sinet,cost,t)

Output:

How to Plot 3D in MATLAB (5)

Check this link for more details about the plot3() function.

Use the quiver3() Function to Create a 3D Quiver or Vector Plot in MATLAB

Matlab’s built-in function quiver3() can be used to plot a 3D quiver or vector plot. A scalar consists of only magnitude, but a vector contains magnitude and direction.

We can create a 3D plot of scalar data using the plot3() function, but we cannot plot vector data using the plot3() function because the function will not plot the direction of the vector.

In the case of vector, we can use the quiver3() function, which will plot the magnitude and direction of the vector.

For example, let’s create a matrix and plot it on a 3D plane with direction. See the code below.

clcm = [1:10 ;10:-1:1;1:10];quiver3(m,m,m,m)

Output:

How to Plot 3D in MATLAB (6)

Check this link for more details about the quiver3() function.

Use the bar3() Function to Create a 3D Bar Graph in MATLAB

We can use Matlab’s built-in function bar3() to plot a bar graph in a 3D plane. We must pass the data’s input matrix, plotted as heights on the z-axis in a 3D plane.

The other two coordinates (x and y) will be taken from the indices of the given matrix. For example, let’s create a 3D bar graph from a given matrix.

See the code below.

clcm = [1:10 ;10:-1:1;1:10];bar3(m)

Output:

How to Plot 3D in MATLAB (7)

Check this link for more details about the bar3() function.

How to Plot 3D in MATLAB (2024)

FAQs

How do you plot a 3D model? ›

In order to plot 3D figures use matplotlib, we need to import the mplot3d toolkit, which adds the simple 3D plotting capabilities to matplotlib.
  1. import numpy as np from mpl_toolkits import mplot3d import matplotlib.pyplot as plt plt.
  2. fig = plt. ...
  3. x = [1, 2, 3, 4] y = [3, 4, 5] X, Y = np.

How to plot f xyz in MATLAB? ›

Description. fimplicit3( f ) plots the 3-D implicit function defined by f(x,y,z) = 0 over the default interval [-5 5] for x , y , and z . fimplicit3( f , interval ) specifies the plotting interval for x , y , and z . fimplicit3( ax ,___) plots into the axes specified by ax instead of into the current axes.

Which command is used to plot a 3D surface in MATLAB? ›

surf( X , Y , Z ) creates a three-dimensional surface plot, which is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y .

How to make a 3D model in MATLAB? ›

Accepted Answer

In MATLAB, the patch function can be used to generate a 3D model by specifying the vertices and faces of the object. This function provides a convenient way to plot and visualize 3D objects in MATLAB. patch('Vertices', vertices, 'Faces', faces, 'FaceColor','red');

How to plot three variables in MATLAB? ›

Direct link to this comment
  1. x = randn(1,20); % Create Data Vector.
  2. y = randn(1,20); % Create Data Vector.
  3. z = randn(1,20); % Create Data Vector.
  4. xv = linspace(min(x), max(x), numel(x)); % Interpolatioon Vector.
  5. yv = linspace(min(y), max(y), numel(y)); % Interpolation Vector.
  6. [X,Y] = ndgrid(xv,yv); % Interpolation Matrices.
Mar 29, 2023

How to plot a 3D graph in MATLAB using Excel data? ›

The following example will illustrates how to do that:
  1. % Read the data points as a matrix.
  2. T = readmatrix('3DPlot.xlsx');
  3. % Create a meshgrid for plotting.
  4. [xq,yq] = meshgrid(0:.2:11.5, 0:.2:7.6);
  5. % Interpolate the points using griddata function.
  6. vq = griddata(t(:, 1), t(:, 2), t(:, 3),xq,yq);
  7. % Plot the surface.
Nov 23, 2022

How do you plot XYZ coordinates? ›

In the same way that we plot points in two-dimensional coordinate space by moving out along the x-axis to our x value, and then moving parallel to the y-axis until we find our point, in three-dimensional space we'll move along the x-axis, then parallel to the y-axis, then parallel to the z-axis until we arrive at our ...

What is the formula for plot in MATLAB? ›

plot( X 1, Y 1,..., X n, Y n) plots multiple pairs of x- and y-coordinates on the same set of axes. Use this syntax as an alternative to specifying coordinates as matrices. plot( X 1, Y 1, LineSpec 1,..., X n, Y n, LineSpec n) assigns specific line styles, markers, and colors to each x-y pair.

Can you plot 3-D in MATLAB? ›

This example shows how to create 3-D line plots in MATLAB using the plot3 function. Create a regularly-spaced vector t from 0 to 10*pi using pi/50 as the increment between elements.

What is 3-D visualization in MATLAB? ›

Plot data on 3-D globe, create 3-D relief maps, drape data over terrain. 3-D visualization functions enable you display and explore geographic data. This figure compares 3-D visualizations for similar regions in Colorado. The image on the left is a globe display with terrain data from a DTED file.

How to plot 3-D cylinder in MATLAB? ›

To draw the cylinder, pass X , Y , and Z to the surf or mesh function. [X,Y,Z] = cylinder( r ) returns the x-, y-, and z- coordinates of a cylinder with the specified profile curve, r , and 20 equally spaced points around its circumference.

How to plot 3D cylinder in MATLAB? ›

To draw the cylinder, pass X , Y , and Z to the surf or mesh function. [X,Y,Z] = cylinder( r ) returns the x-, y-, and z- coordinates of a cylinder with the specified profile curve, r , and 20 equally spaced points around its circumference.

How to make a 3D pie chart in MATLAB? ›

Create 3-D Pie Chart
  1. x = [1,3,0.5,2.5,2]; figure pie3(x) fig2plotly(gcf, 'TreatAs', 'pie3');
  2. x = [1,3,0.5,2.5,2]; figure pie3(x) explode = [0,1,0,0,0]; figure pie3(x,explode) fig2plotly(gcf, 'TreatAs', 'pie3');
  3. x = 1:3; labels = {'Taxes','Expenses','Profit'}; figure pie3(x,labels) fig2plotly(gcf, 'TreatAs', 'pie3');

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Reed Wilderman

Last Updated:

Views: 6340

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.