# How to Plot a 3D Contour plot in Python?

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 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.contour3D` method.

### 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.contour3D` method.

### 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.contour3D` method.

### 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.contour3D` method 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 –