# Plotting 3D Plots in Matplotlib

November 24, 2020 2020-12-03 21:18## Plotting 3D Plots in Matplotlib

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 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()

**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()

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.