3 minutes fast guide for Subplots & Connection Patch in Matplotlib

Ayush Raj
The STEM
Published in
3 min readOct 1, 2021

--

Ever wondered what does subplot(2 1 1) and subplot(2 1 2) means in matplotlib or you can stack multiple graphs together with zoom effect ? If yes, we got you covered. Know about Matplotlib ,Pyplot, subplot and Connection Patch.

Introduction

Hey readers , today we’ll be going through an important aspect of an important library used in data visualization .Yes ,you guessed it right it’s Matplotlib.

Matplotlib is a powerful python library for plotting graphs. Pyplot ,a Matplotlib module, provides a MATLAB-like interface with the ability to use Python, and the advantage of being free and open-source. So lets dive in further.

1.Plotting four graphs of same size together using subplot

Code for 4 plots stacked together ( made on carbon)
Fig 1: 4 Graphs stacked together as subplots side by side (made on author’s Jupyter notebook)

Now let’s understand what we have written . plt.subplot(2, 2, 1) means the plot has a size of 4 (no of rows=no of columns=2) and 1 means 1st graph. These are subplot grid parameters encoded as a single integer. For example, “111” means “1x1 grid, first subplot” and “234” means “2x3 grid, 4th subplot”. Alternative form for add_subplot(111) is add_subplot(1, 1, 1).

Fig 2 : Subplot numbering and representation

We can get the graphs iteratively using loops.

Code for iterative approach (made on carbon)

2.Plotting five graphs of different size together using subplot

An extension to this is what if we want to make subplot with graphs of different size. In the code below plt.subplot(3,2,1) forms 3*2 grid and from that we use subplot 1,3 and 5 which falls on left while plt.subplot(2,2,2) forms 2*2 grid and from that we use subplot 2, and 4 which falls on right. Unused overlapping region of subplots are used by one another.

Fig 3: different sized graphs stacked using subplots (plotted on author’s pc on Jupyter notebook)

3. Plotting three graphs together using subplot in with Zoom effect

We will combine the power of subplot and connection patch to plot a region of a graph .

Coding part( made on carbon)
Fig 4: Zoom effect using Connection Patch (made on author’s pc on jupyter notebook)

Steps used

1.Create main container with required size ,here 6x5

2.Create first axes, the top-left plot with green plot

3.Create second axes, the top-left plot with orange plot

4.Create third axes, a combination of third and fourth cell

5.Create highlighted area in third axes : “sub3.fill_between((1,2), -1.5, 1.5, facecolor=’green’, alpha=1)” means fill green color between x=1 & x=2 and y=-1.5 &y=1.5 ; alpha denotes intensity.

6.Create left side of Connection patch for first axes and add to the left side of figure : “con1 = ConnectionPatch(xyA=(1, 0.755), coordsA=sub1.transData,
xyB=(1, 0.755), coordsB=sub3.transData, color = ‘green’)”means create a connection (line ) between subplot sub1 to sub3 of green color linking cordinate (1,0.755) in both subplots. We add this subplot using add_artist function to figure. “fig.add_artist(con1)”

7.Repeat this 6th steps 3 more times.

Comment and share if you liked. I’m open to constructive criticism.

End of blog…

--

--