Python – Plotting with Object-Oriented API

Learn how to plot a correlation matrix using pyplot and object-oriented APIs in this article by Srinivasa Rao Poladi, the co-founder of krtrimaIQ a consulting firm that provides cognitive solutions to create tomorrow’s Intelligent Enterprises powered by automation, big data, machine learning, and deep learning.

Matplotlib has a three-layer architecture. The top layer is called the scripting layer, and is implemented with pyplot()  API. This API has been designed for users without much programming experience, so it shields the user from the complexities of creating and rendering the plots using the bottom two layers (artist and backend).

For complex applications that use advanced features, such as patches, collections, interactive plotting using events, callbacks, animation, and widgets, we will have to use an object-oriented API. However, it requires quite a bit of Python coding experience to leverage full potential of this API. As new versions of Matplotlib get released, more and more of these features are moving into the pyplot  API, reducing the dependence on an object-oriented API.

The name object-oriented API is a bit confusing, as it may give the impression that the pyplot  API is not an object-oriented, which is not true. It only means that an object-oriented API uses artist and backend layers directly, bypassing the scripting layer (without using the pyplot  API). This gives the flexibility to use complete functionality offered by Matplotlib, whereas the pyplot  API offers a subset of it.

It should be noted that for any plot or figure, it need not be the pyplot  API or an object-oriented API; it can be a mix of both. When we have to use a complex grid structure to place plots in a figure, we use an object-oriented API. When plotting each graph individually and creating figure objects and underlying canvas, we use the pyplot  API. Once we see the differences between the two APIs, you will understand this combined use of both the APIs.

Plotting a correlation matrix using pyplot and object-oriented APIs

In this article, you will learn the difference between pyplot  and an object-oriented APIs. We will plot the same correlation matrix, first with the pyplot  API and then with an object-oriented API. You can find the complete code for this article at https://github.com/PacktPublishing/Matplotlib-3.0-Cookbook/tree/master/Chapter05. We will use an example of wine quality dataset for plotting the correlation matrix. You can find this dataset at https://github.com/PacktPublishing/Matplotlib-3.0-Cookbook/blob/master/Chapter02/winequality.csv.

So, let’s get started!

Getting ready

Import the following libraries for the pyplot  API:

Import the following libraries for an object-oriented API:

matplotlib.backends  has a set of backends that it supports. Here, we are importing FigureCanvasAgg , which provides the space for plotting the figure and maps it to the defined backend device.

IPython.core.display  enables displaying the plot onto the output device.

IPython.core.display enables displaying the plot onto the output device.

How to do it…

The following code block reads the data using pandas and plots the correlation matrix using the pyplot  API.

  • Read the data from a CSV file into pandas DataFrame:

  • Get a correlation matrix of all attributes of wine_quality:

  • Define the figure with its size and plot the image and associated color bar:

  • Set the title and ticks for the figure and display it on the screen:

The following code block plots the same correlation map using an object-oriented API (without using the pyplot API):

  • Read the data from a CSV file into pandas DataFrame:

  • Get a correlation matrix of all attributes of wine_quality:

  • Define the figure with its size:

  • Attach the figure to the canvas:

  • Define the axes on the figure, where we need to plot a correlation map, and plot the correlation map and colorbar:

  • Set ticksand ticklabels:

  • Display the figure on the screen:

How it works…

The pyplot  API code block is self-explanatory. Here is the output chart:

Here is the explanation for the object-oriented API code block:

  • Figure(figsize=(12,9)) instantiates the figure object with a size of (12, 9).
  • FigureCanvas(fig) attaches the canvas (area where the figure needs to be plotted, which ultimately gets displayed on the connected backend device). In the case of the pyplot API, we don’t have to do this, as the pyplot API takes care of it.
  • fig.add_subplot(111) creates the axis object within the figure object created earlier. Note that we can create multiple axes objects within one figure object. Here, we are creating only one axis, as we have only one graph to plot.
  • axs.imshow(corr,cmap=’hot’) creates the correlation matrix as an image, using the standard colormap, hot.
  • fig.colorbar(corimage) attaches the color bar to the image created before.
  • To create ticks and ticklabels for both x and y axes, the pyplot API has the xticks and plt.yticks methods that take both tick positions and ticklables as arguments and manage both with one command. But, in the case of an object-oriented API, we will have to do it separately.
  • set(xticks=range(len(corr)), yticks=range(len(corr)), title=’Correlation Matrix’) defines x and y axis ticks and title for the figure. It needs as many ticks as there are attributes in the correlation matrix.
  • fontd is the dictionary with all the attributes to be applied on xticklabels. We can do the same for yticklabels also, if we want.
  • set_xticklabels(corr.columns, fontdict=fontd) sets the xticklabels with defined attributes.
  • set_yticklabels(corr.columns) sets ticklabels for the y axis.

If you found this article interesting, you can explore Matplotlib 3.0 Cookbook to build attractive, insightful, and powerful visualizations to gain quality insights from your data. Matplotlib 3.0 Cookbook is your hands-on guide to exploring the world of Matplotlib, and covers the most effective plotting packages for Python 3.7.

Srinivasa Rao Poladi has been in the IT services industry for over two decades, providing consulting and implementation services in data warehousing, business intelligence, and machine learning areas for global customers. He has worked with Wipro Technologies for two decades and played key leadership roles in building large technology practices and growing them to multi-million $ business. He spoke at international conferences, published many blogs and white papers in the areas of big data, business intelligence, and analytics.

Tagged with: ,