【问题标题】:Change subplot size in python matplotlib在 python matplotlib 中更改子图大小
【发布时间】:2016-08-10 11:09:43
【问题描述】:

我想在使用 Python 的 matplotlib 时更改 subplot 的大小。我已经知道如何更改 figure 的大小,但是,如何更改相应子图本身的大小?这在我的谷歌搜索中仍然有些难以捉摸。谢谢。

(例如,给定一个图,其中我们有 2x4 子图。我想让图中的子图占据更多区域)。

【问题讨论】:

    标签: python numpy matplotlib plot figure


    【解决方案1】:

    您还可以使用轴大小。 http://matplotlib.org/api/axes_api.html

    set_position() 可用于更改轴的宽度和高度。

    import matplotlib.pyplot as plt
    
    ax = plt.subplot(111)
    
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 1.1 , box.height * 1.1])
    

    【讨论】:

    • 能否举个例子说明这一点?
    • 我添加了一个简单的例子。虽然没有测试。我希望它可以帮助你:)
    • Thaks,我一直在寻找这样的东西。然而,当我有一个来自 imshow 的图和另一个来自 plt 的图时,它有一个奇怪的行为,并且由于比例没有保持,所以调整了图形的大小。
    【解决方案2】:

    您可以使用.subplots_adjust(),例如

    import seaborn as sns, matplotlib.pyplot as plt 
    
    titanic = sns.load_dataset('titanic')
    
    g = sns.factorplot(x='sex',y='age',hue='class',data=titanic,
                      kind='bar',ci=None,legend=False)
    g.fig.suptitle('Oversized Plot',size=16)
    plt.legend(loc='best')
    g.fig.subplots_adjust(top=1.05,bottom=-0.05,left=-0.05,right=1.05)
    

    导致这个怪物:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2015-08-15
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多