我遇到了图例和错误栏绘图命令的相当奇怪的行为。我正在将Python xy 2.7.3.1与matplotlib 1.1.1 以下代码一起使用,以例证所观察到的行为:
import pylab as P
import numpy as N
x1=N.linspace(0,6,10)
y1=N.sin(x1)
x2=N.linspace(0,6,5000)
y2=N.sin(x2)
xerr = N.repeat(0.01,10)
yerr = N.repeat(0.01,10)
#error bar caps visible in scatter dots
P.figure()
P.subplot(121)
P.title("strange error bar caps")
P.scatter(x1,y1,s=100,c="k",zorder=1)
P.errorbar(x1,y1,yerr=yerr,xerr=xerr,color="0.7",
ecolor="0.7",fmt=None, zorder=0)
P.plot(x2,y2,label="a label")
P.legend(loc="center")
P.subplot(122)
P.title("strange legend behaviour")
P.scatter(x1,y1,s=100,c="k",zorder=100)
P.errorbar(x1,y1,yerr=yerr,xerr=xerr,color="0.7",
ecolor="0.7",fmt=None, zorder=99)
P.plot(x2,y2,label="a label", zorder=101)
P.legend(loc="center")
P.show()
这产生了这个情节:
如您所见,错误栏上限正在覆盖散点图。如果我增加zorder足够多,这种情况将不再发生,但情节线将覆盖图例。我怀疑该问题与matplotlib的zorder问题有关。
快速,肮脏,hacky解决方案也受到赞赏。
编辑(感谢@nordev):期望的结果如下:
误差线和末端盖应在散点图点以下。
线图应在散点图和误差线上方
传说应高于一切
根据您的答案调整zorder:
P.legend(zorder=100)
-> self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'zorder'
P.errorbar(zorder=0)
,P.scatter(zorder=1)
,...如正确地你的建议,仍然得到同样的情节,误差线帽仍然是分散的点上方。我相应地更正了上面的示例。
饮歌长啸
慕盖茨4494581
相关分类