Matplotlib Multi-Column, Row Spanning Layouts

..go beyond the standard grid layout in subplots

Balaji Sundararaman
Towards Data Science

--

Image by Gerd Altmann from Pixabay

For Visualization in Python, Matplotlib library has been the workhorse for quite some time now. It has held its own even after more nimble rivals with easier code interface and capabilities like seaborn, plotly, bokeh etc. have arrived on the scene. Though Matplotlib may lack the interactive capabilities of the new kids on the block, it does a more than adequate job of visualizing our data exploration tasks in Exploratory Data Analysis(EDA).

During EDA, one may come across situations where we want to display a group of related plots as part of a larger picture to drive home our insight. The subplot function of matplotlib does the job for us. However, in certain situations, we may want to combine several subplots and want to have different aspect ratios for each subplot. How can we achieve this layout, where, essentially some subplots span across several rows/columns of the overall figure?

Enter gridspec submodule of Matplotlib.

We first need to create an instance of GridSpec which allows us to specify the total number of rows and columns as arguments in the overall figure along with a figure object.

We store the GridSpec instance in a variable called gs and specify that we want to have 4 rows and 4 columns in the overall figure.

Now, we need to specify the details of how each subplot will span the rows and columns in the overall figure. It is useful to make a rough sketch on paper as to how you want the subplots to be laid out, so that they don't overlap. Once done, we convey this information through the GridSpec object we created. The row/column span info is passed in the same index notation we use for subsetting arrays/dataframes with rows and column index numbers starting from zero and using the : to specify range. The GridSpec object with the index is passed to the add_subplot function of the figure object.

We add an overall title for the figure and remove the ticks to visualize the layout better as the objective here is to demonstrate how we can achieve subplots spanning multiple rows /columns. When you implement this, obviously you will want to add your axis ticks, labels etc. from your dataframe and tweak the spacing and figure size to accommodate these plot elements.

Boom! This may come in handy in multi-variable time series plots where we may want to show the time series plot stretching across the columns in the top row and other uni-variate, multi-variate visualization in the other subplots below. You can customize how your jigsaw looks like by specifying your row/columns in the overall figure and spans of your individual subplots.

In R, achieving the above is ridiculously easy with the patchwork package in a single line of code with nothing more than + and / operators and ( ) to even have nested subplots if you want to go bonkers. Click on the below link to see how you can get this done in R.

Thanks for reading. If you liked this article, you may also like the one below on how to do EDA with minimal lines of code with maximum output.

Would love to hear your feedback and comments. Thanks!

--

--

Passionate about Data Analytics, Visualization and Machine Learning with extensive experience across functions in India’s emerging Fintech vertical