Today we will be covering the following topics:
1. Meshgrid and Ndgrid
2. Contour Plots
3. Plot 3
4. Surface Plots – Using Surf
5. Mesh Plots – Using Mesh
6. Changing Color of 3D Plots
VIDEO COMING SOON!
Meshgrid and Ndgrid
MATLAB uses the commands meshgrid and ndgrid to go from a 1D array (aka, a vector) to a 2D array (aka, a matrix). This means that if you enter a vector (1D), using these commands will repeat that vector to create a matrix (2D) out of it. Meshgrid is used for 2D and 3D arrays, whereas Ndgrid is used for all 2D, 3D, and higher dimensions.
Example – Meshgrid
The syntax for meshgrid is [X,Y]=meshgrid(x,y) for 2D arrays or [X,Y,Z]=meshgrid(x,y,z) for 3D arrays.
Below is an example using meshgrid for 2D arrays. We first define the vectors x and y (I used the colon operator, which was discussed in a previous lesson. This is a quicker way to create vectors, but you can manually type the numbers to create the vectors as well). x is includes 1 2 3, y includes 1 2 3 4, and [X,Y] = meshgrid(x,y) puts x and y together into an array. Note that although x and y both have different dimensions (x has 3 numbers, y has 4 numbers), using meshgrid creates a matrix of equal dimensions for both x and y. Then when we use it for further calculations, the matrix dimensions agree.
Running this code gives the following. See how x and y are repeating column-wise to create matrices out of the vectors.
Now can evaluate an expression over this 2D grid. Say we want to evaluate X+Y. We add a line for X+Y into the script.
Running this gives the following. Ans at the bottom of the Command Window is the answer. This takes each position from the matrices x and y and adds them together. For example, the first column and first row of both matrices is the number 1. Adding these together makes the first row, first column of the output X+Y as 2. I have highlighted these below:
Example – Ndgrid
The syntax for ndgrid is [X1, X2, …] = ndgrid(x1,x2…) or just [X1,X2,…] = ndgrid(x) if you’d like to use the same x-vector rather than different vectors x1, x2, etc. The difference between ndgrid and meshgrid is that the repeating vectors x and y are now repeated as rows instead of columns, as you will see in the output below. I have used the same vectors x = 1:3 and y = 1:4 as I did with meshgrid.
Running this code gives the vectors x and y now turned into matrices by repeating the vectors row-wise. Notice that with meshgrid, the vectors repeated in columns, but here they repeat in rows:
Example – Higher Dimensions with Ndgrid
Although we just used X and Y with both meshgrid and ndgrid, ndgrid will be able to take higher dimensions past X, Y, Z, which is the limit for meshgrid. This can be seen here. I have made this 5-dimensional, with X, Y, Z, A and B. This would not work in meshgrid, as it only takes up to 3 dimensions.
Contour Plots
Contour means an outline, especially an outline of the form/shape of an object. A contour line is a line along which a function has a constant value. Contour plots are also sometimes called level plots. It is a way to show a 3D surface on a 2D plot. In real life applications, these are lines that join areas of equal elevation, such as a contour plot in geography. This works by connecting the (x,y) coordinates with lines at each of the z-values. The contours (the lines) in the contour plots can be called z-slices or iso-response values.
A contour plot can be made with MATLAB using the contour command. There are many different versions of this command, and we will cover these in the examples below.
In the syntax below, X and Y are the matrices you wish to plot, and Z is the height, or level, of those coordinates. Your variables may not be called X, Y, or Z, but replace the inputs to the contour command according to what you named your variables.
Example – Contour(Z)
This creates a contour plot with the isolines of a matrix. This matrix will replace the Z in the brackets. It should contain the heights of the (x,y) coordinates that are (column, row) indices of Z. The number of contour lines to show are automatically chosen by MATLAB.
The output shows as this graph:
Example – Contour(X,Y,Z)
This is the same as Example 1, but you will enter the matrices X and Y rather than have MATLAB use them from the matrix Z. Here I have created two vectors X and Y, then used meshgrid to make them into arrays. I used the equation sin(X) + cos(Y) and defined it in variable Z. Then I used contour to plot X,Y with a height of Z.
This is the graph that comes up:
Example – Contour(X, Y, Z, levels)
Here you can specify the number of contour lines you want in the levels input. I have used the same code as above, but now specified 10 in the levels.
Notice the number of lines are different in this graph versus the one above.
Example – Contour(X, Y, Z, LineSpec)
Here you can specify the style and color of the contour lines in the LineSpec input. Line colors and styles were discussed in a previous lesson, and will need to be added in single quotes. I have changed the input to the contour command to specify the line color to be red using ‘r’.
This is the output, and the graph is all in red as we had specified. Although this is one aspect of what you can control for aesthetics of the graph, you can specify the line style (such as wanting the line to be dotted or dashed), the thickness, the color, and more. Click here if you missed the lesson on Line Styles.
One quick way to create a contour plot is to use fcontour(function), where you will replace function with a function you want to plot – you can define this as an anonymous function (anonymous functions were covered in a previous lesson). This plots the function over default values -5 to 5 for X and Y.
If we have the following anonymous function defined in terms of X and Y:
We can use fcontour with Z (the anonymous function):
This is the output graph:
Plot3
If we wanted to plot a 3D space, this can be done using a 3D Plotting tool, such as surf, mesh, or plot3.
Plot3 will plot coordinates in 3D space. You need to specify X, Y, and Z as vectors which need to be the same length. You can also plot multiple sets of coordinates if one of the variables is a matrix and the rest are vectors.
Say I have the following three vectors X, Y, Z:
To use plot3, I will call these three vectors in order within the command:
This is the output graph:
Surface Plots – Using Surf
With both surface and mesh plots, the colors are determined by the heights Z, so color is proportional to surface height.
There are different versions of the surf command, and we will cover these in the examples below. Note that the dimensions of the matrices X and Y need to match for surf to work.
Example – Plotting using Surf, with Meshgrid
Let’s plot X+Y below using surf first. We need to create a new variable to store the equation X+Y in, and I have called that variable F. This is the line F = X+Y. I then used it in the surf command – surf(X,Y,F). The order of the inputs into surf need to be the X,Y variables, then the variable you have stored the equation in, which was F in this case. This should be the last variable in the brackets.
This is the plot that comes up when the code is executed:
Example – Plotting with one input Z
It isn’t necessary to specify X and Y coordinates to use surf. If you do not specify these values, you can still use surf with just one input – Z. Recall as I mentioned above, Z is the height of the (X,Y) coordinates. The rows and columns of the matrix Z will be used to replace X and Y, respectively.
This is the output graph:
Mesh Plots – Using Mesh
We can do the same thing with 3D Plotting as we did with surf using mesh instead.
Example – Using Mesh and Meshgrid for 3D Plotting
Running the code gives this graph, which is the same as the surf graph, which isn’t colored in the same way. It does not have black edges nor is it filled in with color. If you specifically want to see the detail inside the shape, use mesh.
Example – Plotting with one input Z
This is exactly like plotting with one input using surf, but we have now done this with mesh.
The output is the same as with surf, but the colors are not filled in with mesh plots:
Changing Color of 3D Plot
There are a couple different ways to change the color scheme of a 3D surface. There are predefined colors, or you can set a specific color that is not predefined. Let’s discuss the following ways to change the color. You will need to have the surface already plotted that you would like to change the color of, then you can apply these techniques on it.
(1) Predefined Colormaps
These predefined colors can be used to change the color of a surface by calling the command colormap.
Example – Using Predefined Colormaps
Say we have this surface above. This is a quick graph you can make by using the following code:
To change the color to the predefined colormap theme spring, we type the following:
This is what the surface color becomes. You can use any of the other predefined colormap schemes to change this to a different color as well!
(2) Specify Number of Colors or Intensity of Colors with Colormap
Each predefined colormap has 256 colors by default, but you can adjust this if you only wish to use a portion of these colors. For this, use the jet command and specify the numbers with colormap.
If I have plotted a surface called f and want it to become multicolored like this, I can use the following syntax to get the graph above:
Now say I only want to use 7 different colors in the image above, rather than the entire colormap palette of 256 colors. I will use the following syntax. The only thing you need to change is the number within the double brackets – I changed it to 7.
Now you will get the color palette between 7 colors, rather than the gradient using 256 colors from the earlier image.
If you want to specify colors, this can be done using an RGB triplet – this is 3 values in a row vector that define a specific color. Each row vector can have values ranging from 0 to 1, with 1 being the brightest hue of that color. Although 1 is the highest, you can use any number between 0 – 1 to make the colors not as bright. Below is a list of common RGB triplets.
Now let’s look at a couple examples.
Example – Making a Red Graph
The RGB triplet for red is [1 0 0] so we can use this for each of the row vectors. If we change the first number in each row to 1, it will make the graph completely red.
If I wanted to change this to a color that isn’t as bright, I can change the 1 to 0.5 instead:
Example – Making a Multicolored Graph
Now let’s make a multicolored graph using different RGB triplets. Let’s use the RGB triplets for yellow, magenta and red. These are respectively [1 1 0], [1 0 1] and [1 0 0] from the earlier table.
Example – Making another Multicolored Graph
You can also change the colors of the graph to make it multicolored by using numbers between 0 – 1 for all the positions, rather than only 0 or 1.
I randomly chose these numbers above, and this is what the graph looks like:
(3) hsv2rgb Command
Another way to change the colors is by specifying hue, saturation, and value (short form HSV) of a surface. Then use the hsv2rgb command to change your specifications into red, green and blue colors. This is similar to what we did above by specifying values from 0 – 1 in the vector. The first position in the column is the hue, the second position is saturation, and the third position is the value. You can have multiple rows, with each row being a different shade.
Example – Multicolored Graph with hsv2rgb command
I have made a variable called hsv and created a vector with 3 rows in it. This vector has 5 columns, but you can have more or less than that to specify different color shades. Each of the three positions within the rows accounts for either hue (underlined in yellow), saturation (underlined in red), or value (underlined in blue).
Then use the hsv2rgb command with the variable you created the vector in, which was the variable hsv in this case. I have assigned this command to the variable newhsv.
Here is a table explaining what the hue, saturation or value positions of the rows within the vectors mean.
Now we will use the colormap command with the newhsv variable.
This created the following graph:
(4) Shading Surfaces
To set color shading properties, use the shading command with different types. These methods are flat, faceted, and interp. Faceted is the default shading mode you will usually see in surfaces if you do not specify anything else.
Example – Shading with interp
Let’s use interp as the type of shading for this example. After you have created a surface you want to shade, type the following into your script or Command Window:
This will shade the surface like so:
(5) Colorbar
After plotting a surface, you can use the colorbar command to display a bar with a range of colors like this:
Type the words colorbar to display this bar next to the graph:
Example – caxis
If you want to change the axis of the colors displayed on your surface, use the caxis command. Notice how the same graph from above now has more yellow in it.
This was because I had used the caxis command with limits for the color bar, like so:
I have set the lower and upper limits as -2 and 2, respectively. Originally, the color bar went from -8 to 8, and you can see the axis on the bar has changed from Image 35 to 36. Notice the colors associated with each number on the color bar – what changing the axis does is change the colors on the graph to correspond with the colors at each value of the bar. For values that are not on the bar, such as 5, but that value is on the graph’s axis, MATLAB will take the highest number on the color bar and set the surface color as the corresponding color. For example, the color bar is yellow at a value of 2. This means that it should also be yellow on the graph. Because 2 is the upper limit, this means anything greater than 2 will be colored yellow as well.