Matplotlib保存图形时在外部添加图例

54 投票
4 回答
78332 浏览
提问于 2025-04-17 10:53

阅读了以下文章后,我成功地把图例放到了图表外面。

代码:

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() 保存为文件时,图例却被截断了。

我在网上查了一些资料,发现了一些解决办法,比如在 savefig() 中添加 bbox_extra_artists=[leg.legendPatch] 或者 bbox_extra_artists=[leg],但都没有效果。

那正确的方法是什么呢?我用的 Matplotlib 版本是 0.99.3。

谢谢。

4 个回答

12

在大多数情况下,一个简单的解决办法是修改一下 plt.savefig() 这个命令:

plt.savefig('your_figure.png', bbox_inches='tight')

这个解决方案已经在问题下面的评论中提到过,但我之前没注意到,后来在 GitHub 的 matplotlib 问题中找到了。把这个答案留给那些没看评论的人。

21

虽然这个方法在使用图例(legend)时有效,但在有多个子图的情况下,如果我们想要一个整体的图例,figlegend似乎就不太好用了。当我们保存图像时,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])
28

问题在于,当你动态绘图时,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')

撰写回答