问题描述
maplotlib绘图边缘留白太多:
import matplotlib.pyplot as plt
plt.subplot(221, fc='r')
plt.subplot(222, fc='g')
plt.subplot(223, fc='b')
plt.subplot(224, fc='c')
plt.savefig('test.jpg')
plt.show()

解决方案
使用plt.savefig方法保存图片时,设置bbox_inches参数为tight(紧凑型):
import matplotlib.pyplot as plt
plt.subplot(221, fc='r')
plt.subplot(222, fc='g')
plt.subplot(223, fc='b')
plt.subplot(224, fc='c')
plt.savefig('test.jpg', bbox_inches='tight')
plt.show()

问题拓展
查阅官方文档可知,当设置bbox_inches参数为tight时,边缘留白也就是边框的宽度你可以通过设置pad_inches的值来自定义,默认是0.1(单位:英寸)

import matplotlib.pyplot as plt
plt.subplot(221, fc='r')
plt.subplot(222, fc='g')
plt.subplot(223, fc='b')
plt.subplot(224, fc='c')
plt.savefig('test.jpg', bbox_inches='tight', pad_inches=1)
plt.show()

引用参考
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html