Introduction to Three-Dimensional Plotting in MatPlotLib
Finally, we have reached the final installment for our MatPlotLib series (at least for a time).The MatPlotLib series has explored a wide variety of topics in the MatPlotLib library. In one regard, we’ve spent a great deal of time elaborating on plot types. In particular, we have discussed in great depths the plotting of simple line plots, scatter plots, plotting density and contour plots, as well as a variety of histogram types. Furthermore, we have devoted significant attention towards customization features with plots. In this capacity, we focused explored on customizing plot legends, customizing plot colorbars, customizing tick marks, and including figure annotations in MatPlotLib. In this final discussion of MatPlotLib features, we return to our extrapolations on MatPlotLib computational graphing by addressing three-dimensional plotting in MatPlotLib. Let’s press onward to examine this subject further.
Three-Dimensional Plotting in MatPlotLib
When it comes to three-dimensional plotting in MatPlotLib, the tools necessary for executing these functions are innately present. Rather, they exist in a tertiary toolkit which must be imported for functional use. Therefore, whenever we seek to plot data in three-dimensions, we begin our scripts by importing the ‘mplot3d’ code from the ‘mpl_toolkits’ library.
Once we have imported the three-dimensional tool kit, the three-dimensional plot is created with rather similar syntax to how we might encode a two-dimensional plot. The difference is that with three-dimensional plots, we specify the projection attribute as ‘3d’.
Here is the code we may employ to procure an empty three-dimensional plot:
The first line imports the three-dimensional toolkit we need to create the plot. We then use the object-oriented methodology for creating a plot using the plt.figure function. Finally, we specify the plot as three-dimensional using the ‘projections’ attribute of the plt.axes function. It makes sense for this projection attribute to be an instance of the axes because a three dimensional plot exhibits three individual axes rather than two.
When we execute this code, we retrieve an empty, three dimensional plot that appears as follows:
As we can see here, the three-dimensional space procured is a rather canonical means of representing mathematical, spatial information.
Plotting Three-Dimensional Lines
In previous discussions of MatPlotLib, some of the basic plot types we explored included line plots and scatter plots. Additionally, we modeled multivariate equations of the form z=f(x,y) utilizing contour and density plots. Here, we apply these concepts of modeling multivariate functions to plotting in three dimensional space.
To plot a multivariate function in three dimensional space, we need to create multiple sets of three-dimensional coordinates. Let us observe the code that creates this three dimensional data and plots it:
Here, we utilize the same code for encoding the three dimensional space. We then establish our input data using the Numpy linspace function. We then pass these values into two functions, sine and cosine functions, which return the output values ‘x’ and ‘y’. Finally, we plot these values in three dimensional space using the plot3D function. The output function appears as follows:
Obviously the axes have not been optimally designed. However, we have efficaciously created a three dimensional plot of our data.
Plotting Three Dimensional Scatter Plots
In addition to the line plot, we can use similar data accession methods to create a scatter plot. This also follows a similar syntax to our plotting of this type in two-dimensions. Let us first take a look at the code for doing so:
Take note of the fact that first, we have decreased the number of points created to 100, as the markers will appear to take up more width than the line. Having less points will facilitate better visibility of the graph’s nature as a three-dimensional scatter plot. Furthermore, rather than using the plot3D function, we use the scatter3D function to plot this three-dimensional data. In executing this code we obtain the following graph:
One interesting aspect to note about this is the fact that MatPlotLib defaults to altering the marker transparency automatically to improve the interpretation of a marker’s depth in the graph.
Three-Dimensional Contour Plots
With contour plots in two dimensions, we utilized two dimensional space to model three dimensional data. When plotting contour plots in three dimensions, we can use these to model three or even four dimensional data.
The syntax for encoding three-dimensional contour plots follows quite closely to methodologies applied in two dimensions. Let’s take a moment to create the three dimensional data we will apply to create this three dimensional contour plot:
Firstly, we define a function which takes as input two parameters. These values pass into a function which returns a particular value. After defining the function, we establish the inputs by using the Numpy linspce function then create a grid of all the (x,y) pairs. We then pass these (x,y) pairs into the function to output the ‘z’ values. Once we do this, we plot the figure using the contour3D function. Let’s take a look at the code that plots the data.
In addition to specifying the input and output data in the contour3D function, we also specify the color using the cmap attribute. All of this code produces the plot that follows:
Three Dimensional Wire Frames
Wireframes are a three dimensional plot type which provide an alternative to the contour plot we coded above.
We can utilize the same input data we created, but rather than using the contour3D function, we use the plot_wireframe function. Let us first take a look at the code employed to develop the wireframe:
When we run this code with our original input data, we obtain a plot that appears as follows:
Note here that the intercalating white space denotes regular polygons.
Plotting Three Dimensional Surface Plots
The syntax for coding three-dimensional surface plots is nearly identical to creating wire frame plots. The only difference is that rather than using the plt_wireframe function, we use the plot_surface function. Again, we can utilize the original input data to create this structure. Let’s then take a look at the code that creates the surface plot:
When we execute this code, the following surface plot is produced:
The Take Away
Our discussion of MatPlotLib features has drawn to a temporary close, and what a topic to end on. The mechanisms of three dimensional plotting are numerous and have a wide variety of applications for computational modeling. The fortunate circumstance is that if a programmer knows the syntax for coding simple two-dimensional plots, then creating cognate three-dimensional plots is quite simple. In fact, the mathematical background behind three dimensional plots is likely the rate-limiting step moreso than the coding itself. Moving forward, much of our series will involve modeling computational features with MatPlotLib as a tertiary subject, so moving forward, having a strong grappling with MatPlotLib is essential. Nevertheless, three-dimensional plotting in MatPlotLib never fails to procure rather professional appearing computational models. If you seek to explore this subject matter further, consider checking out the MatPlotLib manual found here.