MPL 支持图例的元组输入,以便您可以创建复合图例条目(请参见上图的最后一个图)this http://matplotlib.org/1.3.1/users/legend_guide.html#legend-handler页)。然而,截至目前,图例不支持 PolyCollections(fill_ Between 创建/返回),因此简单地提供 PolyCollection 作为图例元组中的条目是行不通的(预计 mpl 1.5.x 会修复 https://github.com/matplotlib/matplotlib/pull/3303).
在修复到来之前,我建议使用代理艺术家 http://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists与“元组”图例输入功能结合使用。你可以使用mpl.patches.Patch
界面(如代理艺术家页面上所示)或者您可以只使用 fill.例如。:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0, 1])
y = x + 1
f, a = plt.subplots()
a.fill_between(x, y + 0.5, y - 0.5, alpha=0.5, color='b')
p1 = a.plot(x, y, color='b', linewidth=3)
p2 = a.fill(np.NaN, np.NaN, 'b', alpha=0.5)
a.legend([(p2[0], p1[0]), ], ['Stuff'])
plt.show()