Data Visualization for absolute beginners[part 2/3]

Matplotlib Object-Oriented Method

Ishank Sharma
Analytics Vidhya
4 min readJun 15, 2020

--

Now that we’ve covered the basics, let’s shift our focus to introduction of Matplotlib’s Object Oriented API. This will allow us to instantiate figure objects and then we can call methods from that object.

Introduction to the Object-Oriented Method

The idea here is to use a more formal Object-Oriented method to create figure objects and then call the respective methods or attributes of that object. This proves to be a wonderful practice when dealing with multiple plots.

We will now begin by creating a figure instance. Then we will add axes to that figure.

NOTE : We did the same thing using plt.plot(x,y) in part 1 of this tutorial. But now using this Object-Oriented approach we are going to have a lot more control over our fig object and we can easily add more than one axes to our figure as shown below.

Subplots()

Now we are going to learn how to create subplots using the same object-oriented approach. Subplot is different because we can specify the number of rows and columns.

The plt.subplots() object acts as a more automatic axis manager.

Basic use cases:

Now we will specify the number of rows and columns while instantiating the subplots() object:

Out[]

Recall that in part 1 we did the same thing by manually giving rows and columns, but since its an array we can actually iterate over it and directly plot the data.

In matplotlib, the location of axes (including subplots) are specified in normalized figure coordinates. It can happen that your axis labels or titles (or sometimes even ticklabels) go outside the figure area, and are thus clipped.

plot with overlapping labels

To solve this problem we can use fig.tight_layout() or plt.tight_layout() method, which on its own adjusts the positions of the axes on the figure canvas to avoid overlapping content:

tight_layout() can take keyword arguments of pad, w_pad, and h_pad. These control the extra padding around the figure border and between subplots. The pads are specified in fraction of font size.

Figure size, aspect ratio, and DPI

Matplotlib allows the aspect ratio, DPI (dots per inch), and figure size to be specified when the Figure object is created. We can use the figsize and dpi keyword arguments.

  • figsize is a tuple of the width and height of the figure in inches
  • dpi is the dots-per-inch (pixels per inch).

Out []:

These same arguments can also be passed tosubplots function:

Saving figures

Matplotlib generates high-quality of output in the following format JPG, PNG, EPS, SVG, PDF, and PGF.

In order to save a file, we will use the savefig method in the Figure class:

We can also optionally specify dots per inch (DPI) and choose different output formats

Congratulations! on completing part 2 of the series head on to part 3 by clicking here

Sources

--

--

Ishank Sharma
Analytics Vidhya

Trying to bring easy access to knowledge + hacks for everyone.