在 matplotlib中紧凑的显示图片--去除图片周边空白

问题:matplotlib 及其他一些画图工具,在保存plot的时候,总会出现一些问题,例如图片周边包含多余空白区域。这里主要解决这个问题.

我们保存plot的图片:

plt.figure(figsize=(15,8)) 
p = plt.subplot(111)
plt.savefig('image_name')

会得到下面的图像,图片周围存在一些不必要的空白区域 redundant padding.


在 matplotlib中紧凑的显示图片--去除图片周边空白_第1张图片
image with redundant padding

这些padding会占用大量的空间,压缩我们关注的图片。去掉这些padding:

plt.figure(figsize=(15,8)) 
p = plt.subplot(111)
plt.savefig('image_name', ,bbox_inches='tight')

bbox的意思是bounding box, tight就不用说了,让保存的图片更加紧促。可以得到:


在 matplotlib中紧凑的显示图片--去除图片周边空白_第2张图片
image with padding region removed

需要注意的是:bbox_inches ='tight' 只能用于保存图片,不能用于显示。

你可能感兴趣的:(在 matplotlib中紧凑的显示图片--去除图片周边空白)