【发布时间】:2012-02-16 19:32:41
【问题描述】:
阅读下面的文章,我设法把一个传说放在情节之外。
代码:
import matplotlib.pyplot as pyplot
x = [0, 1, 2, 3, 4]
y = [xx*xx for xx in x]
fig = pyplot.figure()
ax = fig.add_subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width*0.8, box.height])
ax.plot(x, y)
leg = ax.legend(['abc'], loc = 'center left', bbox_to_anchor = (1.0, 0.5))
#pyplot.show()
fig.savefig('aaa.png', bbox_inches='tight')
pyplot.show() 显示正确的图,其外带有图例。但是当我将它保存为带有fig.savefig() 的文件时,图例会被截断。
一些谷歌搜索向我展示了解决方法,例如将 bbox_extra_artists=[leg.legendPatch] 或 bbox_extra_artists=[leg] 添加到 savefig(),但都没有奏效。
正确的做法是什么? Matplotlib 版本为 0.99.3。
谢谢。
【问题讨论】:
-
(我看到这是旧线程,但它是谷歌的第一个)有一个更好的解决方案,包括演员到 savefig:stackoverflow.com/questions/10101700/…
-
使用
fig.savefig('aaa.png', bbox_inches='tight', bbox_inches="tight")就像@MPa 建议在@dparker 指出的问题(stackoverflow.com/questions/44642082/…)刚刚为我工作
标签: python matplotlib legend