Python——plot绘制图像,保存图像时去除图像边缘的空白区域

正常的使用plot绘制图像时,边缘都会存在空白边框,保存的结果可能不满足需求,所以想要去除周围的空白边框。之前查了一些资料,发现找到的方法上仍存在不足,之后尝试并总结了一下代码,成功地实现了该功能。代码如下:

plt.axis('off')
plt.gcf().set_size_inches(512 / 100, 512 / 100)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=0.93, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.savefig('image.png')

很多资料中都只是单纯的用了subplots_adjusts()或者margins(),需要将两个函数都用上才能完全地去除空白边框。

你可能感兴趣的:(python学习)