How can rows in a plot have different numbers of columns?

Question

In the context of this exercise in Matplotlib, how can rows in a plot have different numbers of columns?

Answer

In order to understand how rows of a plot can have different numbers of columns, we first need to understand how the subplot essentially works.

When you apply subplot, you are not actually setting permanent dimensions for the layout of the plot grid.

Instead, think of it as though each time you use subplot, with some specified values, you are just creating a “virtual” or temporary grid layout placed on top of the entire plot area. After we place the subplot based on the layout of this grid, it is removed until we create a new subplot, with a new temporary grid.

For example, if we create a subplot as follows
ax1 = plt.subplot(2, 2, 1)
this will create a temporary 2x2 grid, and place the subplot at index 1 based on this 2x2 grid, which is the upper left.

And, if we wanted to add another subplot, say at the bottom right, we would do
ax2 = plt.subplot(2, 2, 4)
which again creates a temporary 2x2 grid, and places the subplot at index 4, which is the lower right area.

A way to visualize this would be

# [temp ] is used to signify temporary empty subplots
# when the virtual grid is applied.

# Adding ax1
[ ax1 ] [temp ]
[temp ] [temp ]

# Result plot
[ ax1 ]


# Adding ax2
[temp ] [temp ]
[temp ] [ ax2 ]

# Result plot
[ ax1 ] 
        [ ax2 ]

What essentially happens when we create a subplot is as follows:

It creates a temporary “virtual” grid on top of the entire plot area.

It places the subplot within the layout of that temporary grid at the specified index, based on that grid.

After the subplot is placed, that virtual or temporary grid disappears, and we have the subplots on the plot area, as we intended.

When we add another subplot, this process repeats.

5 Likes

Hi,

Just confirming for my knowledge: as we specify a 2x2 grid but only create one subplot in the first row, does that subplot automatically (and always) consume all of the space available in that row until another subplot is created in that row?

such as in the exercise, a 1x2 subplot in the top row and 2 1x1 subplots in the bottom row?

3 Likes

I found this article helpful: https://towardsdatascience.com/subplots-in-matplotlib-a-guide-and-tool-for-planning-your-plots-7d63fa632857

12 Likes

I love the clarity of this explanation.

2 Likes

Does the ‘virtual’ grid without rows and columns?

So helpful :exploding_head: thank you! :upside_down_face:

1 Like

so, in this exercise, I can just use plt.subplot(1, 1, 1) for the first subplot and it would be the same since the 2nd row contains nothing yet and a new grid will be created when you call subplot again anyway?

Super helpful! Thanks :slight_smile:

Don’t think so, or at least, it does not work for me - first plot is gone.

What worked for me was working with (2,2,(1,2)) for the same subplot.

The result doesn’t look like this, (the image in the exercise description):

image

But rather looks like this: