Three-dimensional Plotting in Python using Matplotlib

05 Jun, 2021
Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit. In this article, we will deal with the 3d plots using matplotlib.Example:  
Python3
import numpy as npimport matplotlib.pyplot as plt  fig = plt.figure()ax = plt.axes(projection ='3d')
Output: 
With the above syntax three -dimensional axes are enabled and data can be plotted in 3 dimensions. 3 dimension graph gives a dynamic approach and makes data more interactive. Like 2-D graphs, we can use different ways to represent 3-D graph. We can make a scatter plot, contour plot, surface plot, etc. Let’s have a look at different 3-D plots. 
Graph with lines and point are the simplest 3 dimensional graph. ax.plot3d and ax.scatter are the function to plot line and point graph respectively.Example 1: 3 dimensional line graph  
Python3
# importing mplot3d toolkits, numpy and matplotlibfrom mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt fig = plt.figure() # syntax for 3-D projectionax = plt.axes(projection ='3d') # defining all 3 axesz = np.linspace(0, 1, 100)x = z * np.sin(25 * z)y = z * np.cos(25 * z) # plottingax.plot3D(x, y, z, 'green')ax.set_title('3D line plot geeks for geeks')plt.show()
Output: 
Example 2: 3 dimensional scattered graph  
Python3
# importing mplot3d toolkitsfrom mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt fig = plt.figure() # syntax for 3-D projectionax = plt.axes(projection ='3d') # defining axesz = np.linspace(0, 1, 100)x = z * np.sin(25 * z)y = z * np.cos(25 * z)c = x + yax.scatter(x, y, z, c = c) # syntax for plottingax.set_title('3d Scatter plot geeks for geeks')plt.show()
Output: 
 
Surface graph and Wireframes graph work on gridded data. They take grid value and plot it on three-dimensional surface.Example 1: Surface graph  
Python3
# importing librariesfrom mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt # defining surface and axesx = np.outer(np.linspace(-2, 2, 10), np.ones(10))y = x.copy().Tz = np.cos(x ** 2 + y ** 3) fig = plt.figure() # syntax for 3-D plottingax = plt.axes(projection ='3d') # syntax for plottingax.plot_surface(x, y, z, cmap ='viridis', edgecolor ='green')ax.set_title('Surface plot geeks for geeks')plt.show()
Output: 
Example 2: Wireframes  
Python3
from mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt  # function for z axeadef f(x, y):    return np.sin(np.sqrt(x ** 2 + y ** 2)) # x and y axisx = np.linspace(-1, 5, 10)y = np.linspace(-1, 5, 10)  X, Y = np.meshgrid(x, y)Z = f(X, Y) fig = plt.figure()ax = plt.axes(projection ='3d')ax.plot_wireframe(X, Y, Z, color ='green')ax.set_title('wireframe geeks for geeks');
Output: 
 
Contour graph takes all the input data in two-dimensional regular grids, and the Z data is evaluated at every point.We use ax.contour3D function to plot a contour graph.Example:  
Python3
from mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt  # function for z axisdef f(x, y):    return np.sin(np.sqrt(x ** 2 + y ** 3)) # x and y axisx = np.linspace(-1, 5, 10)y = np.linspace(-1, 5, 10)X, Y = np.meshgrid(x, y)Z = f(X, Y) fig = plt.figure()ax = plt.axes(projection ='3d') # ax.contour3D is used plot a contour graphax.contour3D(X, Y, Z)
Output: 
 
The above graph is sometimes overly restricted and inconvenient. So by this method, we use a set of random draws. The function ax.plot_trisurf is used to draw this graph. It is not that clear but more flexible.Example:  
Python3
from mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as plt  # angle and radiustheta = 2 * np.pi * np.random.random(100)r = 6 * np.random.random(100) # all three axesx = np.ravel(r * np.sin(theta))y = np.ravel(r * np.cos(theta))z = f(x, y) ax = plt.axes(projection ='3d')ax.scatter(x, y, z, c = z, cmap ='viridis', linewidth = 0.25); ax = plt.axes(projection ='3d')ax.plot_trisurf(x, y, z, cmap ='viridis', edgecolor ='green');
Output: 
 
Möbius strip also called the twisted cylinder, is a one-sided surface without boundaries. To create the Möbius strip think about its parameterization, it’s a two-dimensional strip, and we need two intrinsic dimensions. Its angle range from 0 to 2 pie around the loop and width ranges from -1 to 1.Example:  
Python3
from mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib.tri import Triangulation  theta = np.linspace(0, 2 * np.pi, 10)w = np.linspace(-1, 5, 8)w, theta = np.meshgrid(w, theta)phi = 0.5 * theta # radius in x-y planer = 1 + w * np.cos(phi) # all three axesx = np.ravel(r * np.cos(theta))y = np.ravel(r * np.sin(theta))z = np.ravel(w * np.sin(phi)) # triangulate in the underlying# parameterizationtri = Triangulation(np.ravel(w), np.ravel(theta))  ax = plt.axes(projection ='3d')ax.plot_trisurf(x, y, z, triangles = tri.triangles,                cmap ='viridis', linewidths = 0.2);
Output: 
 
anikaseth98
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
Python Dictionary
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Taking input in Python
Enumerate() in Python
Read a file line by line in Python
Python String | replace()