Matplotlib savefig 在图外有图例
阅读下面的文章,我设法将图例放在情节之外。
代码:
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。
谢谢。
Reading the following article, I managed to put a legend outside plot.
code:
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()
displays the correct plot with a legend outside it. But when I save it as a file with fig.savefig()
, the legend is truncated.
Some googling shows me workarounds such as adding bbox_extra_artists=[leg.legendPatch]
or bbox_extra_artists=[leg]
to savefig()
, but neither worked.
What is the correct way to do it? Matplotlib version is 0.99.3.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是,当您动态绘图时,matplotlib 会自动确定边框以适合所有对象。
当您保存文件时,事情不会自动完成,因此您需要指定
图形的大小,然后是坐标区对象的边界框。
以下是更正代码的方法:
The problem is that when you plot dynamically,
matplotlib
determines the borders automatically to fit all your objects.When you save a file, things are not being done automatically, so you need to specify
the size of your figure, and then the bounding box of your axes object.
Here is how to correct your code:
虽然此方法适用于图例,但当有多个子图并且我们想要一个整体图例时,它似乎不适用于Figlegend。保存fig时figlegend仍然被裁剪。我只是将我的临时解决方案粘贴在下面,以防有人遇到这种情况。
Although this method works with legend, it seems not to be working well with figlegend when there are multiple subplots and we want a single overall legend. figlegend still get cropped when savefig. I just pasted my temporary solution below in case someone faces such a case.
对于大多数情况,一个简单的修复案例是修改
plt.savefig()
命令:plt.savefig('your_figure.png', bbox_inches='tight')
这问题下面的评论中已经建议了解决方案,但我忽略了这些,并在 GitHub。为未阅读顶部评论的其他人保留此答案。
For most cases, a simple fix cases is to amend the
plt.savefig()
command:plt.savefig('your_figure.png', bbox_inches='tight')
This solution has already been suggested in the comments below the question, but I overlooked those and found it in the matplotlib issues on GitHub. Keeping this answer for others who didn't read the comments on top.
如果一切都失败了,我会使用 Inkscape 的边界框功能来处理 matplotlib 输出中所谓的持久错误。如果您运行的是 GNU/Linux,只需将 Matplotlib 为您提供的任何内容保存为 pdf,然后将其发送到以下地址
If all else fails, I use Inkscape's bounding-box features to deal with what I would call persistent bugs in matplotlib's output. If you're running GNU/Linux, just save whatever Matplotlib gives you as a pdf, and then send it to the following