How do the subplot indices work in Python?

Viewed 391

I'm trying to make sense how the subplot indices work but they don't seem intuitive at all. I particularly have an issue with the third index. I know that there are other ways to create subplots in python but I am trying to understand how subplots written in such a manner work because they are used extensively.

I am trying to use a trivial example to see if I understand what I'm doing. So, here's what I want to do:

  1. Row 1 has 3 columns
  2. Row 2 has 2 columns
  3. Row 3 has 3 columns
  4. Rows 4 and 5 have 2 columns. However, I want to have the left subplot span rows 4 and 5.

This is the code for the first 3 rows. I don't understand why the third index of ax4 is 3 instead of 4.

ax1 = plt.subplot(5,3,1)
ax2 = plt.subplot(5,3,2)
ax3 = plt.subplot(5,3,3)

ax4 = plt.subplot(5,2,3)
ax5 = plt.subplot(5,2,4)

ax6 = plt.subplot(5,3,7)
ax7 = plt.subplot(5,3,8)
ax8 = plt.subplot(5,3,9)

For the three subplots that sit in rows 3 and 4, I can't seem to be able to do that. Here's my wrong attempt:

ax9  = plt.subplot(4,2,10)
ax10 = plt.subplot(5,2,12)
ax11 = plt.subplot(5,2,15)
1 Answers

The indices are from left to right, and then wrap at the end of the row. So subplot(2, 3, x):

1 2 3
4 5 6

For your example, ax4=subplot(5, 3, x) the subplots are indexed:

1 2 3
4 5 6
7 8 9
10 11 12 
13 14 15

For the ax4=subplot(5, 2, x) they are indexed:

1 2
3 4
5 6
7 8
9 10

To span subplots, you can input the start and stop indices:

ax9 = plt.subplot(5, 2, 7:9)
ax10 = plt.subplots(5, 2, 8:10)
Related