Basics of beautiful 3D plotting in MATLAB

Subarna Lamsal
7 min readApr 27, 2019

MATLAB (Matrix Laboratory) is a multi-paradigm numerical computing environment and proprietary programming developed by MathWorks. It supports numerous mathematical equations and manipulations, matrices, function plots, user interfaces, and even interfacing with programs written in C++, Fortran, Octave, Python and many more.

One of the best features of MATLAB is the use of 3D plotting to create beautiful visualisation of functions and relationships between them. It offers 3-dimensional plotting functions such as plot3, comet3, mesh, surf, surfl, contour, ezsurf, fsurf etc. We will begin with a basic plot 3-D line plot. It’s the same as 2-D plot with addition of 3rd vector (array).

z=1:0.01:20;
x=sin(z);
y=cos(z);
plot3(x,y,z);

Hmm… A spiral. Let’s make a cylinder with some minor modifications in code.

z=1:0.01:1000;  %More number of points to make a cylinder
x=sin(z);
y=cos(z);
plot3(x,y,z);
A 3D cylinder using plot3 function

We can use plot3 to plot any 3 vectors in this way Also, we can add titles, labels, legends, xaxis, yaxis, thickness and many more like we add them to 2D plot.

Now we are going to see comet3 plot. A comet plot is an animated graph in which a circle (the comet head) traces the data points on the screen. The comet body is a trailing segment that follows the head. The tail is a solid line that traces the entire function.

Let’s see the same code. But this time we use comet3 instead of plot3.

z=1:0.01:50; % Tracing upto 1000 takes longer time
x=sin(z);
y=cos(z);
comet3(x,y,z);
comet plot

Here, I am not able to embed 3D video of comet3 plot.However, you can visualise the same in http://www.tutorialspoint.com/matlab/try_matlab.php by typing the same code.

Now, we will go towards mesh and surface plots. These plots allows us to plot the data as a surface. Keep in mind that we can also use mesh plot to plot a 2D matrix. If we do this, x and y values are the dimensions of the matrix, and z value is the value at x, y in the matrix. For example, consider the following matrix:

z=[1:5;2:6,3:7]z= 1 2 3 4 5                   % 1:5    2 3 4 5 6                   %2 to 6   3 4 5 6 7                   % 3 to 7

When I do mesh(z), the x axis is 1, y axis is 5 and z is the value at each point on the surface being plotted. The surface will look like this:

mesh plot

The above graph is created by connecting all the points defined in z(the matrix) into a rectilinear grid. Here, x goes from 1 to 3( i.e the number of rows), y goes from 1 to 5 (i.e the number of columns) and z from 3 to 7.

Similarly, surface plots are mesh plots in coloured form. Just a simple one word and see the difference.

surf(z)
surf plot

Again, the x and y cordinates are the numbers of rows and columns and z values in the matrix are those plotted as the surface plot.

We can also control the shading of surface plot using the shade command. The image above shows the default shading i.e flat faceted. We can get cooler surface maps using shading interp. Let’s see the above image with shading interp with the addition of one line of code

shading interp
surf plot with shading maps

Boom!! Colourful and appealing.

Other frequently used shading examples are:

shading flat 
shading faceted
shading interp

Now, let’s delve more into amazing and beautiful 3-D visualisations.

We can get cooler surface maps using peaks and surfl. Try the following:

z=peaks(5);  %You can choose other values and see the difference
surfl(z); % surface plot with color-based lighting

Let’s add more flavor to it.

shading interp
colormap colorcube
Looks really neat

A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the colormap defines the kth color used to identify the intensity of RED, BLUE , and GREEN color.

Other colormap options are :

white    bone      prism    cool    hot
winter summer spring autumn flag
copper pink hsv hot jet

Let’s check one extra colormap image:

colormap flag

One thing to remember using colormap is that some of these options might create an image that will completely devalue the graph, so it is advisable to use them according to the shape and nature of the graph. For an example, if a simple plot3 is used to make a light coloured graph, and if we use colormap white, it will result in null graph.

Now let’s move towards contour plot. Sometimes for the ease of presentation , 3D figures need to be redesigned into 2D plots. Contour makes a 2D representation of a 3-dimensional surface. It takes a matrix, and then chooses the number of contour levels automatically based on the values in the matrix. Let’s look at the following code:

z=peaks(15);
% contour(z, number_of_contour_levels)
contour(z,5); % 5 different contour levels

Let’s not forget colormapping.

colormap hsv

As I told earlier, if we use colormap white in this figure, we will get a null white graph. So, it needs to be taken care of.

Let’s try shaded contour colored pseudo colorplots — rectangular array of cells colour.

z=peaks(30);
pcolor(z); %pseudocolor plot

We can clearly see that an array of cells is created. Based on intensity, colour is placed. To remove the cells and see a more clearer image, we can use:

shading interp
Horray!!

Now, let’s move towards ezsurf and fsurf:

ezsurf creates a graph of function(x,y) using the surf function.Keep in mind that x and y are symbolic variables.This function is plotted over the domain -2*pi <x < 2*pi , -2*pi < y < 2*pi

Let’s try the following code:

syms x y  %Symbolic variables need to be declared
ezsurf(‘x²+y²’);

It is good to remind that it is same as that of the surf plot.

Let’s add some shading and colormapping to it.

shading interp;
colormap spring;

Voila!! A 3D coloured graph representing the equation x²+y²

Finally, let’s proceed towards fsurf plotting. It creates surface plot for symbolic variables like that of ezsurf, however, the constraints ranges from -5 to 5. Let’s look at the following code:

syms x;
fsurf(tan(x));

The striking difference between fsurf and ezsurf is that fsurf tries to use more dense mesh to create graph compared to ezsurf.

Let’s add more flavour to it.

shading interp
colormap hsv

If we use the same code for ezsurf with same shading and colormapping, we get different results. See the difference between above and below graph.

  • Important: The graph from ezsurf consists of different sections whereas fsurf integrates all sections to one and displays a single graph

Let’s use fsurf for functions:

funx=@(a,b) sin(a).*cos(b); %Function 1
funy = @(a,b) sin(a).*sin(b); %Function 2
funz=@(a,b) cos(b); %Function 3
fsurf(funx,funy,funz,[-2*pi 2*pi -2*pi 2*pi]); %parameter of a and b

I have left the job of colormapping for you on this figure.

That’s it. The basics of beautiful 3D plotting using MATLAB. We have covered some important 3D functions and their use. We can use these 3D functions to create more beautiful graphs without loss of content. Cheers!!

--

--