- Object-oriented way of using matplotlib – 1. The Reasons
- Object-oriented way of using matplotlib – 2. MATLAB style versus OO style
- Object-oriented way of using matplotlib – 3. Basics of Object Oriented APIs
- Object-oriented way of using matplotlib – 4. Multiple Subplots
- Object-oriented way of using matplotlib – 5. Customizing Plots, Legend
- Object-oriented way of using matplotlib – 5. Customizing Plots, Ticks
- Object-oriented way of using matplotlib – 5. Customizing Plots, Colorbar
- Object-oriented way of using matplotlib – 6. Artist and Useful Links
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:
- fig.add_subplot()
- fig.add_axes()
- 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.