问题是,当您动态绘图时,会
matplotlib自动确定边框以适合所有对象。保存文件时,操作不会自动完成,因此需要指定图形的大小,然后指定轴对象的边界框。这是更正代码的方法:
import matplotlib.pyplot as pyplotx = [0, 1, 2, 3, 4]y = [xx*xx for xx in x]fig = pyplot.figure(figsize=(3,3))ax = fig.add_subplot(111)#box = ax.get_position()#ax.set_position([0.3, 0.4, box.width*0.3, box.height])# you can set the position manually, with setting left,buttom, witdh, hight of the axis# objectax.set_position([0.1,0.1,0.5,0.8])ax.plot(x, y)leg = ax.legend(['abc'], loc = 'center left', bbox_to_anchor = (1.0, 0.5))fig.savefig('aaa.png')


