【问题标题】:Pandas plot savefig, control frame size not using bbox_inches='tight'Pandas 绘制 savefig,不使用 bbox_inches='tight' 控制帧大小
【发布时间】:2016-11-22 00:34:42
【问题描述】:

绘制数据框时:

d_f.plot()
plt.savefig(image.png, bbox_inches='tight')

bbox_inches='tight' 可用于彻底删除最终 .png 图像中的大量框架(空白)。

是否可以只减少一点帧?

【问题讨论】:

    标签: python python-2.7 python-3.x pandas matplotlib


    【解决方案1】:

    bbox_inches='tight'plt.tight_layout 都是自动的。

    您应该改用plt.subplots_adjust()

    ax = df[['y','w']].plot()
    fig = ax.get_figure()
    left  = 0.125  # the left side of the subplots of the figure
    right = 0.9    # the right side of the subplots of the figure
    bottom = 0.1   # the bottom of the subplots of the figure
    top = 0.9      # the top of the subplots of the figure
    wspace = 0.2   # the amount of width reserved for blank space between subplots
    hspace = 0.2   # the amount of height reserved for white space between subplots
    # These two can be called on 'fig' instead of 'plt' too
    plt.subplots_adjust(left=left, bottom=bottom, right=right, top=top,
                    wspace=wspace, hspace=hspace)
    plt.savefig('image.png')
    

    如果你想左边没有空格,设置left=0,如果你不想右边没有空格,设置right=1

    这是一个夸张的例子,左边没有空格,右边很多,facecolor设置为红色,所以我们可以清楚地看到:

    fig.subplots_adjust(left=left, bottom=bottom, right=right, top=top,
                wspace=wspace, hspace=hspace)
    plt.savefig('image.png', facecolor='red')
    

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 2014-10-30
      • 2013-10-20
      • 2012-10-15
      • 2013-04-08
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多