
In this example, I first set the random seed to 1 so that you can reproduce the same results as me. Here is an example using the first method.
PYPLOT SUBPLOT CODE
It is possible to rotate plots and even create animations via code but that is out of the scope of this article. To save a static version of the plot, click the save icon.

If you right-click and move the mouse, you will zoom in and out of the plot. This lets you manually rotate them by clicking and dragging. The easiest way to rotate 3D plots is to have them appear in an interactive window by using the Jupyter magic command %matplotlib notebook or using IPython (which alwaysĭisplays plots in interactive windows). If you plot this in IPython or an interactive Jupyter Notebook window and you rotate the plot, you will see that the transparency of each point changes as you rotate. # Create 3 samples from normal distribution with mean and standard deviation of 1 If you sample a normal distribution and create a 3D plot from it, you get a ball of points with the majority focused around the center and less and less theįurther from the center you go. Here’s a cool plot that I adapted from this video. 3D plots can seem daunting at first so my best advice is to go through the code line by line.Ĭreating a scatter plot is exactly the same as making a line plot but you call ax.scatter instead. Clearly, this plot is much less smooth than the original and hopefully gives you an understanding of what is happening under the hood with these plots. # Create space of numbers for cos and sin to be applied toįor this plot, I set the third argument of np.linspace to 25 instead of 200. Here’s an example of the power of 3D line plots utilizing all the info above. Check your Python version in Jupyter Notebook.You can change the orientation by clicking and dragging (right click and drag to zoom in) which can really help to understand your data.Īs this is a static blog post, all of my plots will be static but I encourage you to play around in your own Jupyter or IPython One amazing feature of Jupyter Notebooks is the magic command %matplotlib notebook which, if ran at the top of your notebook, draws all your plots in an interactive window.

Hand, they are more complicated since we are so used to 2D plots. In some ways 3D plots are more natural for us to work with since we live in a 3D world. All the functions you know and love such as ax.plot() and ax.scatter() accept the same keyword arguments but they now also accept three positional arguments – X, Y and Z. 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. 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ī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. 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.

In addition to import matplotlib.pyplot as plt and calling plt.show(), to create a 3D plot in matplotlib, you need to: Matplotlib 3D Plot - A Helpful Illustrated Guide How do you plot an interactive 3D plot in Python?.How do you plot a 3D scatter in Python?.
