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

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.

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』.

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.