adjusting heights of individual subplots in matplotlib in Python

后端 未结 2 875
渐次进展
渐次进展 2021-02-05 14:02

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
# ...
相关标签:
2条回答
  • 2021-02-05 14:20

    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

    0 讨论(0)
  • 2021-02-05 14:27

    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.

    0 讨论(0)
提交回复
热议问题