在图表下标记彩色区域

时间:2013-01-10 12:02:59

标签: python graph matplotlib label legend

我希望能够在图例下的彩色区域的图例框中显示标签。着色区域在13 <1之间。 x&lt; 17和22&lt; x&lt; 29

我正在使用:

for i in data.findOne()
    a = [element['total'] for element in i['counts']]
    P.plot(a, label="curve 1", color='green')
    where = np.zeros(len(a),dtype=bool)
    where[13:17] = True
    where[22:29] = True
    P.fill_between(np.arange(len(a)),a,where=where,color='green', alpha='0.5')

P.legend()
P.show()

我可以在哪里插入命令来显示它的图例?我希望阴影区域的图例与曲线的图例位于同一个图例框中。

谢谢!

这就是它的样子:

example

1 个答案:

答案 0 :(得分:2)

当前标签机制不支持由fill_between返回的PolyCollection。你可以做的是创建一个任意的补丁作为代理艺术家,并将其添加为占位符,例如:

from matplotlib.patches import Rectangle
import numpy as np
import pylab as P

xs = np.arange(0,10,0.1)
line1 = P.plot(xs,np.sin(xs),"r-", label="lower limit")[0]
line2 = P.plot(xs,np.sin(xs-1)+3,"b-", label="upper limit")[0]
P.fill_between(xs,np.sin(xs), np.sin(xs-1)+3,color='green', alpha=0.5, label="test")
rect = Rectangle((0, 0), 1, 1, fc="g", alpha=0.5)
P.legend([line1, line2, rect], ["lower limit", "upper limit", "green area"])
P.show()

给我们: Sample

供参考,请参阅this