15

if I have a series of subplots with one column and many rows, i.e.:

plt.subplot(4, 1, 1) # first subplot
plt.subplot(4, 1, 2) # second subplot
# ...

how can I adjust the height of the first N subplots? For example, if I have 4 subplots, each on its own row, I want all of them to have the same width but the first 3 subplots to be shorter, i.e. have their y-axes be smaller and take up less of the plot than the y-axis of the last plot in the row. How can I do this?

thanks.

  • Use tight_layout. This link has good examples: http://matplotlib.org/users/tight_layout_guide.html . To make it work, you need to change the figsize e.g. `plt.figure(figsize=(10, 1.5*plotsToPlot))` where `plotsToPlot` is the number of subplots on one figure. – Dilawar Mar 12 '15 at 11:41
  • Use the [AxesGrid toolkit](http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#axesgrid). – thevilledev Jul 25 '10 at 17:08

2 Answers2

24

Even though this question is old, I was looking to answer a very similar question. @Joe's reference to AxesGrid, was the answer to my question, and has very straightforward usage, so I wanted to illustrate that functionality for completeness.

AxesGrid functionality provides the ability create plots of different size and place them very specifically, via the subplot2grid functionality:

import matplotlib.pyplot as plt

ax1 = plt.subplot2grid((m, n), (row_1, col_1), colspan = width)
ax2 = plt.subplot2grid((m, n), (row_2, col_2), rowspan = height)

ax1.plot(...)
ax2.plot(...)

Note that the max values for row_n,col_n are m-1 and n-1 respectively, as zero indexing notation is used.

Specifically addressing the question, if there were 5 total subplots, where the last subplot has twice the height as the others, we could use m=6.

ax1 = plt.subplot2grid((6, 1), (0, 0))
ax2 = plt.subplot2grid((6, 1), (1, 0))
ax3 = plt.subplot2grid((6, 1), (2, 0))
ax4 = plt.subplot2grid((6, 1), (3, 0))
ax5 = plt.subplot2grid((6, 1), (4, 0), rowspan=2)
plt.show()

Last Graph, twice the height

Jason S
  • 184,598
  • 164
  • 608
  • 970
benjaminmgross
  • 2,052
  • 1
  • 24
  • 29
4

There are multiple ways to do this. The most basic (and least flexible) way is to just call something like:

import matplotlib.pyplot as plt
plt.subplot(6,1,1)
plt.subplot(6,1,2)
plt.subplot(6,1,3)
plt.subplot(2,1,2)

Which will give you something like this: Unequal Subplots

However, this isn't very flexible. If you're using matplotlib >= 1.0.0, look into using GridSpec. It's quite nice, and is a much more flexible way of laying out subplots.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 1
    I believe this answer to sub-optimal,the answer below should be the accepted one – NameOfTheRose Aug 21 '15 at 17:16
  • 2
    @user2422503 - Note that date and the reference to exactly what the answer below suggests. At that time, subplot2grid/GridSpec/etc was very new and most people were using a matplotlib release that didn't contain it. Therefore, I showed the more widely-available solution and gave a pointed to the newer, cleaner solution. – Joe Kington Aug 21 '15 at 20:02
  • @ Joe Kington , I am sorry – NameOfTheRose Aug 22 '15 at 03:44