本文共 698 字,大约阅读时间需要 2 分钟。
最近在作图时需要将输出的图片紧密排布,还要去掉坐标轴,同时设置输出图片大小。但是发现matplotlib使用plt.savefig()
保存的图片周围有一圈空白。那么如何去掉该空白呢?
首先,关闭坐标轴显示:
plt.axis('off')
但是,这样只是关闭显示而已,透明的坐标轴仍然会占据左下角位置,导致输出的图片偏右。
要想完全去掉坐标轴,需要改为以下代码:plt.axis('off')fig = plt.gcf()fig.set_size_inches(7.0/3,7.0/3) #dpi = 300, output = 700*700 pixelsplt.gca().xaxis.set_major_locator(plt.NullLocator())plt.gca().yaxis.set_major_locator(plt.NullLocator())plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)plt.margins(0,0)fig.savefig(out_png_path, format='png', transparent=True, dpi=300, pad_inches = 0)
即可完成去掉空白。
注:如果不采用subplot_adjust + margin(0,0),
而是在fig.savefig()的参数中添加bbox_inches = 'tight'
,也可以达到
去除空白的效果; 但是,这样会导致对图片输出大小的设置失效。
转载地址:http://fvvin.baihongyu.com/