
Then you need to pass projection='3d' which tells matplotlib it is a 3D plot.įrom now on everything is (almost) the same as 2D plotting.

If you just want a single Axes, pass 111 to indicate it’s 1 row, 1 column and you are selecting the 1st one.

You set up your Figure in the standard way fig = plt.figure()Īnd add a subplots to that figure using the standard fig.add_subplot() method. This imports a 3D Axes object on which a) you can plot 3D data and b) you will make all your plot calls with respect to. If you are not comfortable with Figure and Axes plotting notation, check out this article to help you.īesides the standard import matplotlib.pyplot as plt, you must also from mpl_toolkits.mplot3d import axes3d. If you are used to plotting with Figure and Axes notation, making 3D plots in matplotlib is almost identical to creating 2D ones.
#MATPLOTLIB 3D SCATTER PLOT STYLE CODE#
Just execute the code and look at the generated “plot.png” file: Try it yourself with our interactive Python shell. # Plot using Axes notation and standard function callsĪwesome! You’ve just created your first 3D plot! Don’t worry if that was a bit fast, let’s dive into a more detailed example. Plot it using Axes notation and standard function callsĪx = fig.add_subplot(111, projection='3d').Initialize your Figure and Axes3D objects.

In addition to import matplotlib.pyplot as plt and calling plt.show(), to create a 3D plot in matplotlib, you need to:
