5 MATLAB 3D Plot Examples Explained with Code and Colors (2024)

5 MATLAB 3D Plot Examples Explained with Code and Colors (1)

Did you ever wonder seeing amazing 3D graphs in MATLAB? How to draw multiple 3D plot graphs in MATLAB?

This is an in-depth tutorial for you. I will explain the different MATLAB 3D plot examples and how to draw them.

This tutorial is an extension of a previous tutorial two-dimensional [2D] MATLAB plot.

When I share the 2D plot graph tutorial, some of the readers asked me about the 3D plot. And I decided to write about it.

This tutorial provides you the plot’s functions, syntax, and code, for example for the five main different types of 3D plots. At the end of this post, you will be able to draw your own 3D plot graph in MATLAB.

It’s amazing. Right?

Let’s start.

Table of Contents

3D MATLAB Plot Introduction

In general, the three-dimensional plots consist of the three vectors (x,y,z) in the same graph.

In MATLAB, the plot3() function is used to draw the 3D plot graph. You can also use a specified line style, marker, and color for drawing 3D plots.

The general syntax to display the 3D plot is,

plot3(x,y,z)plot3(x,y,z,Name)plot3(x,y,z,LineSpec)

Let’s start drawing different types of the 3D plot graph…

Classifications of Three-Dimensional Plots | MATLAB 3D plot Examples

Here, we are considering, the five main different types of three-dimensional (3D) plots. These graphs are mostly used in the industry.

The following list of different 3D plots as,

  1. Mesh Plot
  2. Surface Plot
  3. Ribbon PLot
  4. Contour Plot
  5. Slice Plot

As a part of this tutorial about MATLAB 3D plot examples, I am describing the topmost five 3D plots one-by-one.

1. Mesh 3D Plot in MATLAB

The mesh plotting function is used to display the mesh plot. It produces a wireframe surface where the lines connecting the defining points are colored.

How to create the Mesh plot in MATLAB?

For the mesh plotting in MATLAB, you need to pass the array values to the mesh function.

Syntax:

Mesh function transforms the domain specified by vectors (X, Y, Z) into arrays (x,y,z).

The syntax for the Mesh Plot is,

mesh(x,y,z)[X,Y,Z] = meshgrid(x,y,z)

MATLAB Code:

As an example, we are plotting the mesh 3D plot for square root mathematical function.

[x,y] = meshgrid(-10:0.1:10); t = sqrt(x.^2+y.^2);z =(10*sin(t));mesh(x,y,z)

Output in MATLAB:

See here, you get a colorful and smooth connecting surface line of three-dimensional [3D] Mesh plot.

5 MATLAB 3D Plot Examples Explained with Code and Colors (2)

You can also plot the graph for various Mathematical Expressions in MATLAB.

2. Surface 3D Plot in MATLAB

A surface plot is somewhat similar to a mesh plot. The main difference between them is, in the surface plot, the connecting lines and the faces both will be displayed in the dark color.

How to create the Surf plot in MATLAB?

Syntax:

In the surface plot, ‘surf’ function is used. So, you can write a simple format like ‘function name(array)’.

surf(x,y,z)surf(z)

MATLAB Code:

Let’s write a MATLAB code for the three-dimensional surface plot for an exponential function exp().

[x,y] = peaks(30);z = exp(-0.9*(x.^2+0.5*(x-y).^2));surf(x,y,z);xlabel('\bf X axis');ylabel('\bf Y axis');zlabel('\bf Z axis');title('\bf Surface Plot')colorbar

Output in MATLAB:

After the getting output of surface plot, you will see the connecting lines and the faces are both displayed in the same shade.

5 MATLAB 3D Plot Examples Explained with Code and Colors (3)

3. Ribbon 3D Plot in MATLAB

As the name ribbon, this 3D plot graph will be having different color ribbons.

How to create the ribbon plot in MATLAB?

Here, we are using ribbon() function for plotting ribbon 3D MATLAB plot.

Syntax:

The general syntax for writing code,

ribbon(x,y,z)ribbon(x,y)ribbon(z)

MATLAB Code:

To create a ribbon plot using peak function for mathematical function ((x²)-(y²))

[x,y] = peaks(30);z =[(x.^2)-(y.^2)];ribbon(z);title('\bf Ribbon Plot')

Output in MATLAB:

You can see each and every colorful shade ribbons.

5 MATLAB 3D Plot Examples Explained with Code and Colors (4)

4. Contour 3D Plot in MATLAB

How to create the three dimensional [3D] contour plot?

To create the three dimensional [3D] contour plot, we are using the ‘contour3’ function.

Note: You can plot the Contour 2D plot by using the only ‘contour’ function.

Syntax:

The syntax for the three-dimensional contour plot,

contour3(x,y,z)contour3(z)

MATLAB Code:

We are plotting the contour plot for the exponential mathematical equation is (exp( x²-y²)).

[x,y] = peaks(30);z = exp(-x.^2-y.^2);contour3(x,y,z);title('\bf Contour Plot')

Output in MATLAB:

Below is a diagram for three dimensional [3D] contour plot.

5 MATLAB 3D Plot Examples Explained with Code and Colors (5)

5. Slice 3D Plot in MATLAB

For plotting slice graph, you must know volumetric data(v), specification of three-dimensional coordinate (x,y,z), and ‘xslice, yslice, zslice’.

Syntax:

Slice plot’s syntax is

slice(x,y,z,v,xslice,yslice,zslice)slice(v,xslice,yslice,zslice)

Where,

  • xslice- ‘x’ coordinate data for slice plot
  • yslice- ‘y’ coordinate data for slice plot
  • zslice- ‘z’ coordinate data for slice plot

MATLAB Code:

Slice plot is little different from other 3D plots types. When you are writing MATLAB code for Slice plot, you need to specify each coordinator value.

Let’s draw the slite plot graph for an exponential mathematical equation.

[x,y,z] = meshgrid(-10:.2:10);v = [exp((x.^2)-(y.^3)-(z.^5))];xslice = 0.1; yslice = 5;zslice = 0;slice(x,y,z,v,xslice,yslice,zslice)colorbartitle('\bf Slice Plot')

Output in MATLAB:

The output looks like the below picture.

5 MATLAB 3D Plot Examples Explained with Code and Colors (6)

These are the topmost three dimensional [3D] used in the industry projects.

This is all about different MATLAB 3D plot examples. I have explained the different classification of MATLAB 3D plots with simple code and syntax.

If you have doubt, write in the comment. I will reply to you as soon as possible.

Other MATLAB Tutorials:

  • MATLAB Math Functions
  • MATLAB M-File Details
  • Matrix in MATLAB
  • Vector in MATLAB
  • MATLAB/ Simulink Toolbox
  • Application of MATLAB/Simulink

Thanks for Reading!

Are you looking for job?

Job openings »

5 MATLAB 3D Plot Examples Explained with Code and Colors (7)

Dipali Chaudhari

I have completed master in Electrical Power System. I work and write technical tutorials on the PLC, MATLAB programming, and Electrical on DipsLab.com portal.

Sharing my knowledge on this blog makes me happy. And sometimes I delve in Python programming.

5 MATLAB 3D Plot Examples Explained with Code and Colors (2024)

FAQs

How to plot 3D graph in MATLAB code? ›

To plot a 3D surface from a data file in MATLAB, you will need to have the data file open in MATLAB. Once you have the data file available, you can use the plot3 command to plot the data. The plot3 command will create a 3D plot of the data. You can also use the surf command to create a 3D surface plot.

How do you plot a graph in different colors in MATLAB? ›

thisIndex = ceil(row/5); thisColor = plotColors(thisIndex, :); plot(thisX, thisY, '-', 'Color', thisColor, 'LineWidth', 2); hold on; % Leave plots up so we'll see all of them at the end.

How to use RGB colors in MATLAB plot? ›

RGB Triplet — Create a custom color by specifying a three-element row vector whose elements are the intensities of the red, green, and blue components of a color. The intensities must be in the range [0,1] . For example, you can specify a shade of pink as [1 0.5 0.8] .

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 plot 3D pattern in MATLAB? ›

Use the patternCustom function to plot the field data in 3-D. This function also allows you to view the sliced data. Alternatively, use the polarpattern object to visualize the field data in 2-D polar format.

How do you use RGB color code? ›

RGB Color Values

For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255), and the other two (green and blue) are set to 0. Another example, rgb(0, 255, 0) is displayed as green, because green is set to its highest value (255), and the other two (red and blue) are set to 0.

How to display color image in MATLAB? ›

To display a truecolor image, call the imshow function or open the Image Viewer app. For example, this code reads a truecolor image into the MATLAB workspace and then displays the image. This sample code uses the variable name RGB to represent a truecolor image in the workspace.

How to plot a 3-D graph? ›

  1. Define a vector-valued function of a single parameter to create a curve in 3D. ...
  2. Insert a 3D plot, type the name of the function in the placeholder without its arguments, and change the Trace Color and the Trace Thickness. ...
  3. Define t as a range variable above the plot.

How to make a 3D model in MATLAB? ›

Direct link to this 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');

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 a 3D graph? ›

  1. Define a vector-valued function of a single parameter to create a curve in 3D. ...
  2. Insert a 3D plot, type the name of the function in the placeholder without its arguments, and change the Trace Color and the Trace Thickness. ...
  3. Define t as a range variable above the plot.

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 to plot 3D implicit function in MATLAB? ›

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.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5685

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.