Plotting 3D Plots in Matplotlib

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

In this chapter, you will learn how to plot 3D Plots in Matplotlib.

A 3D plot is a plot where data is plotted on only the x, y and z-axis. 3D plots are mostly used in simulation and modelling and it is important to know how to plot such Matplotlib plots if you are dealing with numerical analysis in three dimensions. The different types of 3D plots covered in this chapter are:

Let’s get started!

Matplotlib 3D Space Plot – How to make a 3D space plot in Matplotlib?

A Matplotlib 3D Space Plot can be made using the projection property of axes() method of Matplotlib pyplot as shown below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt

# Axes3D is needed for plotting 3D plots
from mpl_toolkits.mplot3d import Axes3D

# For interactive matplotlib sessions, turn on the matplotlib inline mode
%matplotlib inline

# Set projection to 3d in axes() method
plt.axes(projection='3d')

# show() is used for displaying the plot
plt.show()
matplotlib 3d space plot

Matplotlib 3D Line Plot – How to make a 3D line plot in Matplotlib?

A Matplotlib 3D Scatter Plot can be made using the plot3D() function of Matplotlib pyplot.

For plotting a Matplotlib 3D Line Plot, we will have to specify the data for the x-axis, y-axis and z-axis as shown in the example below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt

# Axes3D is needed for plotting 3D plots
from mpl_toolkits.mplot3d import Axes3D

# For interactive matplotlib sessions, turn on the matplotlib inline mode
%matplotlib inline

# Dummy data
x = [1, 2, 3, 4, 5] # X-coordinates
y = [1, 2, 3, 4, 5] # Y-coordinates
z = [4, 10, 20, 5, 3] # Z-ccordinates

# Defining figure
fig = plt.figure(figsize = (8, 6), dpi = 90)

# Making 3D Plot using plot3D()
ax = plt.axes(projection = '3d')
ax.plot3D(x, y, z)

# Setting Axis labels
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_zlabel('Z-Axis')

# Showing the plot
plt.show()
3d plots in matplotlib

Matplotlib 3D Scatter Plot – How to make a 3D scatter plot in Matplotlib?

A Matplotlib 3D Scatter Plot can be made using the scatter3D() function of Matplotlib pyplot.

For plotting a Matplotlib 3D Scatter Plot, we will have to specify the data for the x-axis, y-axis and z-axis as shown in the example below:

# Libraries/Modules import conventions
import matplotlib.pyplot as plt

# Axes3D is needed for plotting 3D plots
from mpl_toolkits.mplot3d import Axes3D

# For interactive matplotlib sessions, turn on the matplotlib inline mode
%matplotlib inline

# Dummy data
x = [1, 2, 3, 4, 5] # X-coordinates
y = [1, 2, 3, 4, 5] # Y-coordinates
z = [4, 10, 20, 5, 3] # Z-ccordinates

# Defining figure
fig = plt.figure(figsize = (8, 6), dpi=90)

# Making 3D Plot using scatter3D()
ax = plt.axes(projection = '3d')
ax.scatter3D(x, y, z)

# Setting Axis labels
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_zlabel('Z-Axis')

# Showing the plot
plt.show()
matplotlib 3D scatter plot

In this chapter, we learned to plot three different kinds of 3D plots in Matplotlib: Space, Line and Scatter Plots.

In the next chapter, we will learn how to save both 2D as well as 3D plots in Matplotlib to local storage. Head over to the next chapter on ‘Saving Plots in Matplotlib‘ and learn how to save plots in Matplotlib.


Plotting 3D Plots in MatplotlibPlotting 3D Plots in Matplotlib

Do you want to learn Python, Data Science, and Machine Learning while getting certified? Here are some best selling Datacamp courses that we recommend you enroll in:

  1. Introduction to Python (Free Course) - 1,000,000+ students already enrolled!
  2. Introduction to Data Science  in Python- 400,000+ students already enrolled!
  3. Introduction to TensorFlow for Deep Learning with Python - 90,000+ students already enrolled!
  4. Data Science and Machine Learning Bootcamp with R - 70,000+ students already enrolled!

Leave a Comment