Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Matplotlib 3D Contour Plot - contour3d() Function

In this tutorial, we will cover the Contour Plot in 3 dimensions using the Matplotlib library.

To draw or to enable the 3d plots you just need to import the mplot3d toolkit.

There is a function named ax.contour3D() that is used to create a three-dimensional contour plot.

This function requires all the input data to be in the form of two-dimensional regular grids, with its Z-data evaluated at each point.

3D Contour Plot Example

In the example given below, we will create a 3-dimensional contour plot for the sine function. The code snippet is as given below:

from mpl_toolkits import mplot3d 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import cm 
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: 

fig = plt.figure() 
ax = plt.axes(projection='3d') 
ax.contour3D(X, Y, Z, 50, 
ax.set_title('3D contour Plot for sine') 

The explanation of the functions used in the above code is as follows:

  • meshgrid

    This is a function of NumPy library that is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian or Matrix indexing.

  • plt.axes()

    This function is used to create the object of the Axes.

  • ax.contour3D

    This function is used to create contour

  • ax.set_xlabel

    This function is used to set the label for the x-axis

  • ax.set_title()

    This function is used to provide a title to the Plot

Following will be the output of the above code:

3d contour plot matplotlib


In the last tutorial, we covered the 3D Line plot and scatter plot and also introduced you to 3D plots in matplotlib. In this tutorial, we covered the 3D Contour plot in matplotlib.