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: t.append(math.sin(math.sqrt(i*2+j*2))) Z.append(t) fig = plt.figure() ax = plt.axes(projection='3d') ax.contour3D(X, Y, Z, 50, cmap=cm.cool) ax.set_xlabel('a') ax.set_ylabel('b') ax.set_zlabel('c') ax.set_title('3D contour Plot for sine') plt.show()
The explanation of the functions used in the above code is as follows:
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.
This function is used to create the object of the Axes.
This function is used to create contour
This function is used to set the label for the x-axis
This function is used to provide a title to the Plot
Following will be the output of the above code:
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.