[英]Matplotlib savefig with a legend outside the plot
阅读以下文章,我设法将图例放在情节之外。
码:
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。
谢谢。
问题是,当您动态绘图时, matplotlib
自动确定边框以适合所有对象。 保存文件时,操作不会自动完成,因此需要指定图形的大小,然后指定轴对象的边界框。 这是更正代码的方法:
import matplotlib.pyplot as pyplot
x = [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
# object
ax.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')
尽管此方法适用于图例,但当有多个子图并且我们需要一个整体图例时,似乎无法与figlegend一起使用。 savefig时,figlegend仍会被裁剪。 我只是在下面粘贴了临时解决方案,以防有人遇到这种情况。
import matplotlib.pyplot as plt
para = {
## this parameter will indicate the position of
## subplot within figure, but will not be shown
## if using bbox_inches='tight' when saving
'figure.subplot.top': 0.5
}
#plt.rcParams.update(para)
fig = plt.figure()
ax=fig.add_subplot(221)
## only needed when what to manually control
## subplot ration
#ax.set_position([0.1,0.6,0.5, 0.4])
ax.plot([1,1,1])
ax=fig.add_subplot(222)
#ax.set_position([0.7,0.6,0.5, 0.4])
ax.plot([2,2,2])
ax=fig.add_subplot(223)
#ax.set_position([0.1,0.1,0.5, 0.4])
ax.plot([3,3,3])
ax=fig.add_subplot(224)
#ax.set_position([0.7,0.1,0.5, 0.4])
p1, = ax.plot([4,4,4])
p2, = ax.plot([2,3,2])
## figlegend does not work fine with tight bbox
## the legend always get cropped by this option
## even add bbox extra will not help
## had to use legend, and manually adjust it to
## arbitary position such as (0.3, 2.5)
## http://matplotlib.org/users/tight_layout_guide.html
## according to this link, tight layout is only
## an experimental feature, might not support figlegend
#lgd = plt.figlend(
lgd = plt.legend(
[p1,p2],
['a', 'b'],
## by default, legend anchor to axis, but can
## also be anchored to arbitary position
## positions within [1,1] would be within the figure
## all numbers are ratio by default
bbox_to_anchor=(-0.1, 2.5),
## loc indicates the position within the figure
## it is defined consistent to the same Matlab function
loc='center',
ncol=2
#mode="expand",
#borderaxespad=0.
)
#plt.show()
plt.savefig('temp.png', bbox_inches='tight')#, bbox_extra_artist=[lgd])
如果所有其他方法都失败了,我将使用Inkscape的包围盒功能来处理我称之为matplotlib输出中的持久性错误。 如果您正在运行GNU / Linux,只需将Matplotlib提供的内容保存为pdf,然后将其发送至以下位置
def tightBoundingBoxInkscape(pdffile,use_xvfb=True):
"""Makes POSIX-specific OS calls. Preferably, have xvfb installed, to avoid any GUI popping up in the background. If it fails anyway, could always resort to use_xvfb=False, which will allow some GUIs to show as they carry out the task
pdffile: the path for a PDF file, without its extension
"""
usexvfb='xvfb-run '*use_xvfb
import os
assert not pdffile.endswith('.pdf')
os.system("""
inkscape -f %(FN)s.pdf -l %(FN)s_tmp.svg
inkscape -f %(FN)s_tmp.svg --verb=FitCanvasToDrawing \
--verb=FileSave \
--verb=FileQuit
inkscape -f %(FN)s_tmp.svg -A %(FN)s-tightbb.pdf
"""%{'FN':pdffile}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.