如何确定matplotlib图例中的项目顺序? [英] How is order of items in matplotlib legend determined?

查看:1000
本文介绍了如何确定matplotlib图例中的项目顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我认为我不必这样做时,我必须对图例中的项目进行重新排序.我尝试:

I am having to reorder items in a legend, when I don't think I should have to. I try:

from pylab import *
clf()
ax=gca()
ht=ax.add_patch(Rectangle((1,1),1,1,color='r',label='Top',alpha=.01))
h1=ax.bar(1,2,label='Middle')
hb=ax.add_patch(Rectangle((1,1),1,1,color='k',label='Bottom',alpha=.01))
legend()
show()

并以中间上方的底部结束.如何获得正确的订单?不是由创建顺序决定的吗?

and end up with Bottom above Middle. How can I get the right order? Is it not determined by creation order?

更新:以下内容可用于强制命令.我认为这可能是最简单的方法,这似乎很尴尬.问题是什么决定了原始订单?

Update: The following can be used to force the order. I think this may be the simplest way to do it, and that seems awkward. The question is what determines the original order?

hh=[ht,h1,hb]
legend([ht,h1.patches[0],hb],[H.get_label() for H in hh])

推荐答案

顺序是确定性的,但是部分私有内容可以随时更改,请参见代码

The order is deterministic, but part of the private guts so can be changed at any time, see the code here (the self.* elements are lists of the artists that have been added, hence the handle list is sorted first by type, second by order they were added).

如果要显式控制图例中元素的顺序,请像在编辑中一样组装处理程序和标签的列表.

If you want to explicitly control the order of the elements in your legend then assemble a list of handlers and labels like you did in the your edit.

这篇关于如何确定matplotlib图例中的项目顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆