Pandas plot savefig,控制框架大小不使用bbox_inches ='紧张'

时间:2016-11-22 00:34:43

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

绘制数据框时:

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

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

是否可以减少一点帧?

1 个答案:

答案 0 :(得分:0)

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

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

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

enter image description here