matplotlib 使用记录

1.保存图片时设置 pad_inches 去掉周围空白部分

在保持图片时,设置 bbox_inches 为 tight
plt.savefig(figpath.pdf', dpi=400, bbox_inches='tight')
可以去掉图片周围的空白部分。这一命令广为人知
但若是在插图到论文,想要进一步放大图片,以便更清晰,还可以进一步使得图片的空白部分更少:
plt.savefig(figpath.pdf', dpi=400, bbox_inches='tight', pad_inches=0.01)
这里 pad_inches 相当于在图片元素四周 pad 的空白像素的大小(单位是 inch)

参考 matplotlib 官方教程

2.调整legend 出现的顺序以协调先前图片或者颜色设置(参见此博客)

handles, labels = ax[2].get_legend_handles_labels()
ax[2].legend(handles[::-1], labels[::-1], loc='upper right')

你可能感兴趣的:(matplotlib 使用记录)