Python 我的matplotlib.pyplot图例被切断

Python 我的matplotlib.pyplot图例被切断,python,matplotlib,Python,Matplotlib,我正在尝试使用matplotlib创建一个侧面带有图例的绘图。我可以看到正在创建绘图,但图像边界不允许显示整个图例 lines = [] ax = plt.subplot(111) for filename in args: lines.append(plt.plot(y_axis, x_axis, colors[colorcycle], linestyle='steps-pre', label=filename)) ax.legend(bbox_to_anchor=(1.05, 1),

我正在尝试使用matplotlib创建一个侧面带有图例的绘图。我可以看到正在创建绘图,但图像边界不允许显示整个图例

lines = []
ax = plt.subplot(111)
for filename in args:
    lines.append(plt.plot(y_axis, x_axis, colors[colorcycle], linestyle='steps-pre', label=filename))
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
这将产生:

编辑:@gcalmetes已发布。
可能应该使用他的解决方案,而不是下面所示的方法。
尽管如此,我还是会留下这个,因为有时候看到不同的做事方式会有所帮助


如中所示,您可以为另一个子地块腾出空间并将图例放置在其中

import matplotlib.pyplot as plt
ax = plt.subplot(121) # <- with 2 we tell mpl to make room for an extra subplot
ax.plot([1,2,3], color='red', label='thin red line')
ax.plot([1.5,2.5,3.5], color='blue', label='thin blue line')
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()
导入matplotlib.pyplot作为plt

ax=plt.subplot(121)#正如Adam所指出的,您需要在图形的一侧留出空间。 如果要微调所需的空间,可以查看matplotlib.pyplot.artist的方法

下面是一个快速示例:

import matplotlib.pyplot as plt
import numpy as np

# some data
x = np.arange(0, 10, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

# plot of the data
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])
ax.plot(x, y1,'-k', lw=2, label='black sin(x)')
ax.plot(x, y2,'-r', lw=2, label='red cos(x)')
ax.set_xlabel('x', size=22)
ax.set_ylabel('y', size=22)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()

生成的图像:

这是另一种创造空间的方法(收缩轴):

其中,0.8将轴的宽度缩放20%。在我的win7 64计算机上,如果图例不在绘图范围内,则使用大于1的因子将为图例腾出空间


此代码引用自:

尽管时间已晚,但我想引用最近引入的一个不错的替代方案:

新matplotlib功能:紧密边界框 如果您对
plt.savefig
的输出文件感兴趣:在这种情况下,标志
bbox\u inches='tight'
是您的朋友

import matplotlib.pyplot as plt

fig = plt.figure(1)
plt.plot([1, 2, 3], [1, 0, 1], label='A')
plt.plot([1, 2, 3], [1, 2, 2], label='B')
plt.legend(loc='center left', bbox_to_anchor=(1, 0))

fig.savefig('samplefigure', bbox_inches='tight')

我还想提及一个更详细的答案:

优势
  • 无需调整实际数据/图片
  • 它与
    plt.子批次
    以及其他不兼容的地方兼容
  • 它至少适用于最常用的输出文件,例如png、pdf

只需使用
plt.tight\u layout()


这可能是在较新的
matplotlib
版本中引入的,并且很好地完成了这项工作。

我知道matplotlib喜欢吹嘘一切都在用户的控制之下,但这整件带有图例的事情太好了。如果我把图例放在外面,我显然希望它仍然可见。该窗口应该只缩放自身以适应,而不是创建这种巨大的重新缩放麻烦。至少应该有一个默认的True选项来控制这种自动缩放行为。强迫用户进行大量的重新渲染,以试图以控制的名义获得正确的比例数字,结果恰恰相反。@strpeter在他的回答中提供了一个自动解决方案,对我来说效果非常好。我看了几分钟这段代码,仍然不知道哪一行解决了问题谢谢!这非常有效,只需要很少的修改。@CarlodelMundo:在您的案例中需要哪些修改?谢谢与我们分享。这应该是plt的默认设置。savefig()工作正常,谢谢!以下是matplotlib:>Bbox中的文档(以英寸为单位),仅供参考。仅保存图形的给定部分。如果是“紧”,请尝试找出图中的紧bbox。如果没有,请使用savefig.bbox^^^ wtf这是否意味着?不管是谁写的这篇文章都会被踢到脸上。谢谢你,我找这篇文章已经很久了!概述了可用于使图例显示在地物边界内的几种技术。
import matplotlib.pyplot as plt

fig = plt.figure(1)
plt.plot([1, 2, 3], [1, 0, 1], label='A')
plt.plot([1, 2, 3], [1, 2, 2], label='B')
plt.legend(loc='center left', bbox_to_anchor=(1, 0))

fig.savefig('samplefigure', bbox_inches='tight')
import matplotlib.pyplot as plt
    
fig = plt.figure(1)
plt.plot([1, 2, 3], [1, 0, 1], label='A')
plt.plot([1, 2, 3], [1, 2, 2], label='B')
plt.legend(loc='center left', bbox_to_anchor=(1, 0))

plt.tight_layout()