In this tutorial, we’ll try to understand how to plot a 3D plot in python.
Matplotlib was initially designed with only two-dimensional plotting in mind. Around the time of the 1.0 release, some three-dimensional plotting utilities were built on top of Matplotlib’s two-dimensional display, and the result is a convenient (if somewhat limited) set of tools for three-dimensional data visualization. three-dimensional plots are enabled by importing the mplot3d toolkit.
Using Axes3D from mplot3d Toolkit
- We can plot a 3-dimensional plot in python using mplot3d toolkit.
- To plot a 3D plot we need three-dimensional axes that can be created by passing
projection='3d'
to any of the normal axes (matplotlibAxes
object). - For the examples in this tutorial, we create 3D Axes (of class Axes3D) by passing the
projection="3d"
keyword argument to Figure.add_subplot.
In simple terms, when we provide the projection='3D'
parameter to the Figure.add_subplot
method, we’re trying to generate 3D axes of class Axes3D that can be used to plot any three-dimensional figure.
Now let us understand the above method using some examples.
Example 1 – Plot 3D axes without any data
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d')
Output:
The steps followed in the above example are:
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
Example 2 – Plot a point in the 3D axes
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') ax.scatter(0,0,0) #plotting a point at (0,0,0) coordinate plt.show()
Output:
The steps followed in the above example are:
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.
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
- Plot a point using
axes.scatter
method.
Example 3 – Plot a continuous curve in the 3D axes
import matplotlib.pyplot as plt import numpy as np fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') z = np.linspace(0, 1, 100) x = z * np.sin(20 * z) y = z * np.cos(20 * z) ax.plot3D(x, y, z, 'gray') plt.show()
Output:
The steps followed in the above example are:
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
- define the variables for 3 different variables
- generate z values using
numpy.linspace
method (refer this).generate x values usingx = zsin(20z)
generate y values usingy = zcos(20z)
- generate z values using
- plot the 3d plot using
axes.plot3D
Example 4 – Create a 3D scatter plot with customizations
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') x = np.random.random(100)*10+20 y = np.random.random(100)*5+7 z = np.random.random(100)*15+50 ax.scatter(x, y, z, 'gray') plt.show()
Output:
The steps followed in the above example are:
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
- define the variables for 3 different variables
- scatter plot the 3d plot using
axes.scatter
Example 5 – Add axes labels and plot title to a 3D plot
import matplotlib.pyplot as plt fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(111, projection='3d') x = np.random.random(100)*10+20 y = np.random.random(100)*5+7 z = np.random.random(100)*15+50 ax.scatter(x, y, z, 'gray') ax.set_title("3D plot") ax.set_xlabel("X axes") ax.set_ylabel("Y axes") ax.set_zlabel("Z axes") plt.show()
Output:
The steps followed in the above example are:
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
- define the variables for 3 different variables
- scatter plot the 3d plot using
axes.scatter
- Set the title and axes labels.
Example 6 – Customize markers in a 3D scatter plot
import matplotlib.pyplot as plt fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(111, projection='3d') x = np.random.random(100)*10+20 y = np.random.random(100)*5+7 z = np.random.random(100)*15+50 ax.scatter(x, y, z, color='red',marker='x') plt.show()
Output:
The steps followed in the above example are:
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
- define the variables for 3 different variables
- scatter plot the 3d plot using
axes.scatter
by modifying the markers.
Example 7 – Plot a 3d polygon
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(111, projection='3d') x = [1, 0, 3, 4] y = [0, 5, 5, 1] z = [1, 3, 4, 0] vertices = [list(zip(x,y,z))] poly = Poly3DCollection(vertices, alpha=0.8) ax.add_collection3d(poly) ax.set_xlim(0,5) ax.set_ylim(0,5) ax.set_zlim(0,5)
Output:
The steps followed in the above example are:
- import required modules
- generate a 3D axes using
figure.add_subplot(projection='3d')
- define the variables for 3 different variables
- Make a 3d polygon collection using
Poly3DCollection
and add the collection to the axes
You might also be interested in –
- How to plot a 3D surface plot in Python?
- How to Plot a 3D Contour plot in Python?
- How to plot a Quiver plot in Python?
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.