【问题标题】:How to create space between subplots? [duplicate]如何在子图之间创建空间? [复制]
【发布时间】:2017-04-23 03:21:59
【问题描述】:

标题几乎说明了一切。 我有一个包含两个子图的笔记本,并想在它们之间创建一些空间。 他们看起来彼此太近了。

【问题讨论】:

    标签: python python-3.x numpy matplotlib subplot


    【解决方案1】:

    与matplotlib.Figure.subplots_adjust:

    import matplotlib.pyplot as plt
    
    fig, (ax1, ax2) = plt.subplots(1, 2)
    
    ax1.plot([1,2,3], [1,2,3])
    ax2.plot([1,2,3], [3,2,1])
    plt.show()
    

    可以通过wspace参数来增加宽度:

    ... # same setup as before
    fig.subplots_adjust(wspace=2)
    plt.show()
    

    如果您想对axes 的定位进行更多控制,则可以将每个轴的偏移(底部和左侧)和延伸(宽度和高度)指定为图形的百分比。

    这需要一些计算才能正确:

    import matplotlib.pyplot as plt
    
    # All have the same lower border, height and width, only the distance to
    # the left end of the figure differs
    bottom = 0.05
    height = 0.9
    width = 0.15  # * 4 = 0.6 - minus the 0.1 padding 0.3 left for space
    left1, left2, left3, left4 = 0.05, 0.25, 1 - 0.25 - width, 1 - 0.05 - width
    
    rectangle1 = [left1, bottom, width, height]
    rectangle2 = [left2, bottom, width, height]
    rectangle3 = [left3, bottom, width, height]
    rectangle4 = [left4, bottom, width, height]
    
    # Create a 8 x 8 (quadratic) figure
    plt.figure(1, figsize=(8, 8))
    
    // Create 4 axes their position and extend is defined by the rectangles
    ax1 = plt.axes(rectangle1)
    ax2 = plt.axes(rectangle2)
    ax3 = plt.axes(rectangle3)
    ax4 = plt.axes(rectangle4)
    
    # Let's display something in these axes.
    ax1.plot([1,2,3,4])
    ax2.plot([4,3,2,1])
    ax3.plot([4,3,2,1])
    ax4.plot([1,2,3,4])
    
    plt.show()
    

    【讨论】:

    • 这缩小了情节。有没有办法只增加空间?
    • @VictorEijkhout 不,因为图形大小是固定的。因此,如果您只想增加空间,则必须增加图形大小和 wspace。
    • 如果有四个,如何在只有两个之间做到这一点?
    • @ItsmeJulian 我更新了答案。一般来说,问一个新问题可能会更好——但我还是想添加一个更通用的方法,所以谢谢你的提醒:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2023-03-28
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多