Object-oriented way of using matplotlib – 4. Multiple Subplots

This entry is part 4 of 8 in the series OO matplotlib





I would like to post a series of articles about object-oriented way of using matplotlib. That explains why we should use that way and how to code like that. The main references are come from the official matplotlib website, “matplotlib.org” and  the book, 『Python Data Science Handbook: Essential Tools for Working with Data, Jake VanderPlas, O’REILLY, 2017』.

In the previous post, I explained the basic objects of object-oriented APIs and showed how to create subplots by using several methods. In this post I will introduce the way of creating multiple subplots.

4. Multiple Subplots

a. Multiple Subplots

I will compare the MATLAB style codes and OO style codes of creating multiple subplots here. The OO style codes use plt.subplots(), fig.add_subplot() and fig.add_axes() methods, while MATLAB style codes use plt.subplot(). That’s a bit cumbersome. To avoid a confusion when you make multiple subplots, I recommend to use OO style creation.

The full source code is here. [Link]

b. Multiple Subplots with Variable Grid Specs

In the following example, you can find the three ways to create variable grid spec multiple subplots. Three ways are:

  1. fig.add_subplot()
  2. fig.add_axes()
  3. fig.add_subplot() by using GridSpec

You can locate the subplots where you want in rows and columns.

The full source code is here. [Link]

We have seen the different ways of creating multiple subplots. In the next post, we are going to find the way to customize legend, ticks and colorbar in OO style.

Series Navigation<< Object-oriented way of using matplotlib – 3. Basics of Object Oriented APIsObject-oriented way of using matplotlib – 5. Customizing Plots, Legend >>

Leave a Comment