 • ## Summary

Search form
• 00:00

[MUSIC PLAYING]

• 00:05

GARY WHITE: To create a 3D contour plot, whichis sort of like the density plotsthat we were doing in the "Getting Started with BasicPlots" section, except now it's goingto be an actual three-dimensional figure.So I've imported NumPy mp.I have imported Matplotlib as plt.And now we have our function, f of x and y,

• 00:27

GARY WHITE [continued]: which creates these e values.So we're going to now specify the actual xvalues that we're going to use to create our plot.So it's going to be x = to np.linspace.And that's going to be from minus 6 to 6.And we're going to want 30 values.

• 00:48

GARY WHITE [continued]: So we're going to duplicate that.And we're going to specify our y values, whichare equal to linearly space values between minus 6 and 6.And again, we're going to have 30 values.So now we're going to create our mesh grid.So we're going to have capital X and capitalY is equal to np.meshgrid.

• 01:12

GARY WHITE [continued]: And we're going to use our X and Y valuesas part of our mesh grid.And then we're going to specify that our Z values areequal to our returns from our function using the X and Yvalues.So we're going to pass X and Y into the function f, and thatwill give us our Z values.So now we can start plotting the values that we've created.

• 01:35

GARY WHITE [continued]: So if we do fig = plt.figure, we can thencreate our axes as well by saying ax = plt.axes.And we need to set our projection = '3D' again.

• 01:60

GARY WHITE [continued]: And then we can start creating our contour plot.So we do ax.contour.And in this case, it's a 3D contour plot.So we need contour 3D.And then we pass in the values that we need.So we need X, Y, Z, in this case, the amount of levels.

• 02:24

GARY WHITE [continued]: So we remember that from the density plot,where we need to specify how many levels isgoing to be in the actual plot.And then we can choose the color map.So in this case, we're going to havea color map equal to binary.And finally, we're just going to set the labelsso ax.set_xlabel.

• 02:49

GARY WHITE [continued]: And we can pass that X. And then we can duplicate that, and setthe Y label equal to Y and the Z label to Z.And finally, we can just use plt.show to seewhat we've plotted.

• 03:14

GARY WHITE [continued]: And so this needs to be a lower case d for 3D.And so here's our contour plot.So you can see the function that's been created in 3D.And we can also change the color mapto the greens instead of binary.

• 03:34

GARY WHITE [continued]: So this gives us a nice green color.And we can see how the function changes in 3D, whereit gets higher near the zero valuesand then also has this cone at the end, which'salso getting higher in the more extreme values.So you can see it's quite a complicated function.And this contour plot gives us a good idea

• 03:55

GARY WHITE [continued]: of where the high and low values are and is perhapsmore intuitive than the density plots that we used before.

### Video Info

Episode: 44

Publisher: Gary White

Publication Year: 2020

Video Type:Tutorial

Methods: Data visualization, Python, Coding

### Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

## Abstract

Gary White explains how to create a 3D contour plot in Matplotlib.