The MatPlotLib Series
This is the Art of Data Science blog, and as you might suspect, the world of Data Science revolves around data. We laboriously discussed techniques of data storage using Pandas in our Pandas series. It’s important you are well versed in this technique of storing data as it is critical that data be in a predictable format for efficient modeling with MatPlotLib. If you need a refresher on this topic of data storage with Pandas, check out the list of topics at the end of this sub-section.
Nevertheless, in this first article of our MatPlotLib series, we elaborate on introductory topics of using MatPlotLib for data modeling. Shall we begin?
- Constructing Pandas Data Structures with COVID-19 Data
- Operating on Pandas Data Structures with Ufuncs
- Using Pandas With Web Scraping
- Vectorized String Operations with Pandas
As with any external Python library, the first order of business to putting these tools to use is downloading said library. You may execute this in the command shell of your CPU. Having downloaded the library, import MatPlotLib into the Python script being written. When importing MatPlotLib, we often abbreviate it as ‘mpl’. Furthermore, the primary function from MatPlotLib for plotting is the ‘pyplot’ function, so we often import this function directly and abbreviate it as ‘plt’. The code for this appears as follows:
Stylistic Control in MatPlotLib
As previously stated, the primary function of MatPlotLib: efficient data modeling through plots. Therefore, much of our efforts with this tool will be the presentation of plots that represent our data.
There are three mechanisms of plotting data using MatPlotLib. including from a Python script, the IPython shell, or the IPython notebook. The use of the Python script is the most general, and thus, will be the method expanded upon herein.
If we are coding in a Python script and seek to reveal our data plot, we can utilize the ‘plt.show()’ function which candidly shows the data plotted in the form specified. However, it’s critical to use this function only once in the script, and will be showed at the end of the script.
The MatPlotLib library was originally designed as an alternative for the MatLab figures. For that reason, much of the syntax for the MatPlotLib functions is quite similar to that as it would be used in MatLab. The MatLab tools are accessible via the pyplot interface, and we can demonstrate how this works. Suppose we have a data set of one hundred values and pass it through a function such as sin(). Also, suppose we plot our data set as the x-axis and the sin of our data set as the y-axis. The code to do so would look like this:
The np.linspace function is a Numpy function which takes inputs for the interval bounds and the number of equally spaced values between those bounds. These numbers are the ‘x’ values. We create two subplots, and pass these ‘x’ values in, while the ‘y’ values are the ‘x’ values passed into a sine or cosine function. The two plots appear as such:
Object Oriented Interface
While the MatLab interface is useful for rapid construction of simple plots, the object oriented interface allows for much more sophisticated programming of plots. The plots created via the object-oriented interface are actual objects which we control via methods. These plotting objects consist of figures and axes. The syntax for creating plots in this fashion is slightly different. Consider the following code:
In this code, we create a figure object by setting ‘fig’ and ‘ax’ equal to the function that creates subplots. We then specify the axes associated with each of the subplots. This results in the same graphs.
One of the benefits of MatPlotLib is the ease by which we save these figures, and flexibility of format. All that one must do to save a figure is use the savefig() function, and specify the file. For example, if we desired to save our previous subplot as a jpeg file, we could execute the following code:
Furthermore, MatPlotLib supports a wide variety of file formats which we can save figures to. These domains include file types such as ‘png’, ‘pdf’, ‘raw’, and many more.
The Take Away
As we have seen, the code for executing MatPlotLib figures is actually quite simple. Furthermore, the tools are quite flexible and permit a great deal of control over figure specification. Hopefully this introduction to the MatPlotLib library gives you a nice opportunity to explore this library. Our future installments of this series will elaborate on much more sophisticated modeling techniques. This will ensure that you yourself may become an expert in implementing these tools. If you desire to explore this topic in greater depth, consider checking out the MatPlotLib manual which may be found here.