In this tutorial, we’ll try to understand how to plot a 3D contour plot in python.
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.
To understand how to plot a 2D contour plot, refer this.
Now to plot a 3D contour plot we can use axes.contour3D.
Using the matplotlib.Axes.contour3D() method
We can plot a 3D contour plot using a matplolib axes object’s contour3D() method. The following is the syntax –
Basic Syntax:
Axes.contour3D(X, Y, Z, *args, extend3d=False, stride=5, zdir='z', offset=None, data=None, **kwargs)
Parameters:
- X, Y, Z: Input data
- extend3d: Whether to extend contour in 3D.
For more details about parameters refer this.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Now let us understand the above method using some examples.
Example 1
import numpy as np
import matplotlib.pyplot as plt
import math
x = [i for i in range(0, 200, 100)]
y = [i for i in range(0, 200, 100)]
X, Y = np.meshgrid(x, y)
Z = []
for i in x:
t = []
for j in y:
t.append(math.cos(math.sqrt(i*2+j*2)))
Z.append(t)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z,50)
plt.show()
Output:

The steps followed in the above example are:
- import required modules
- define the x,y, and z values
- generate a pyplot figure and add a 3D axes
- plot the contour plot in 3D using
Axes.contour3Dmethod.
Example 2
import numpy as np
import matplotlib.pyplot as plt
import math
x = [i for i in range(0, 200, 100)]
y = [i for i in range(0, 200, 100)]
X, Y = np.meshgrid(x, y)
Z = []
for i in x:
t = []
for j in y:
t.append(math.tan(math.sqrt(i*2+j*2)))
Z.append(t)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z,50)
plt.show()
Output:

The steps followed in the above example are:
- import required modules
- define the x,y, and z values
- generate a pyplot figure and add a 3D axes
- plot the contour plot in 3D using
Axes.contour3Dmethod.
Example 3
import numpy as np
import matplotlib.pyplot as plt
import math
def f(x, y):
return np.sin(x) ** 8 + np.cos(20 + y * x) * np.cos(y)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z,50)
plt.show()
Output:

The steps followed in the above example are:
- import required modules
- define the x,y, and z values
- generate a pyplot figure and add a 3D axes
- plot the contour plot in 3D using
Axes.contour3Dmethod.
Example 4
Let’s modify the contour plot theme.
import numpy as np
import matplotlib.pyplot as plt
import math
def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z,50,cmap='binary')
plt.show()
Output:

The steps followed in the above example are:
- import required modules
- define the x,y, and z values
- generate a pyplot figure and add a 3D axes
- plot the contour plot in 3D using
Axes.contour3Dmethod by modifying the cmap parameter (refer this).
Example 5
Let’s now plot the above contour plot with a modified viewing angle.
import numpy as np
import matplotlib.pyplot as plt
import math
def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z,50,cmap='binary')
ax.view_init(60, 35)
plt.show()
Output:

The steps followed in the above example are:
- import required modules
- define the x,y, and z values
- generate a pyplot figure and add a 3D axes
- plot the contour plot in 3D using
Axes.contour3D. - Change the view angle using
Axes.view_init(refer this).
Example 6
Contour plot along with the surface plot.
import numpy as np
import matplotlib.pyplot as plt
import math
def f(x, y):
return np.sin(np.pi*x) * np.sin(np.pi*y)
x = np.linspace(-1, 1, 30)
y = np.linspace(-1, 1, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0.5, rstride=1, cstride=1, alpha = 0.5)
ax.contour3D(X, Y, Z, cmap="binary")
plt.show()
Output:

The steps followed in the above example are:
- import required modules
- define the x,y, and z values
- generate a pyplot figure and add a 3D axes
- plot the surface plot in 3D using
Axes.plot_surface(refer this) . - plot the contour plot in 3D using
Axes.contour3D.
You might also be interested in –
- How to plot a Quiver plot in Python?
- How to Create Heatmaps in Python?
- How to Create a Contour Plot in Matplotlib
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

