按字符串的最后一个字符对压缩列表(字符串,'Line2D' 的实例)进行排序
Sort a zipped list (string,instances of 'Line2D') by the last character of the string
我想对标签列表进行排序,在标签中字符串的最后一个字符之后从 Matplotlib 中的图例中获得句柄。
到目前为止,我尝试结合这个线程:
How is order of items in matplotlib legend determined?
使用此线程:
How to sort a list by last character of string
不幸的是,这没有用。这是我的代码:
handles, labels = ax.get_legend_handles_labels()
handles, labels = zip(*sorted(zip(labels, handles), key = lambda t:[0]))
leg = ax.legend(handles, labels,loc='best', ncol=2, shadow=True, fancybox=True)
基本上什么都不做。
打印输出如下:
print(handles)
print(labels)
[<matplotlib.lines.Line2D object at 0x7fd6182ddcd0>, <matplotlib.lines.Line2D object at 0x7fd6182ddb50>, <matplotlib.lines.Line2D object at 0x7fd6448ddc10>, <matplotlib.lines.Line2D object at 0x7fd6448ddf50>, <matplotlib.lines.Line2D object at 0x7fd6609cb790>, <matplotlib.lines.Line2D object at 0x7fd6609cb190>, <matplotlib.lines.Line2D object at 0x7fd660ac5f10>, <matplotlib.lines.Line2D object at 0x7fd660ac5e90>, <matplotlib.lines.Line2D object at 0x7fd619404d10>, <matplotlib.lines.Line2D object at 0x7fd645fcb9d0>]
['Demo_4 maximum', 'Demo_4 mean', 'Demo_5 maximum', 'Demo_5 mean', 'Demo_6 maximum', 'Demo_6 mean', 'Demo_7 maximum', 'Demo_7 mean', 'Demo_8 maximum', 'Demo_8 mean']
你快到了。需要对行进行一些更正
handles, labels = zip(*sorted(zip(labels, handles), key = lambda t:[0]))
如果您希望收到handles, labels,
,那么您必须按相同的顺序压缩:zip(handles, labels)
按第一个列表排序的键是 lambda t: t[0]
(注意 t)。但是因为我们现在首先有句柄,然后是标签,按标签排序,它将是 lambda t: t[1]
(1 是第二个列表,在本例中是标签)
但是你不想按标签排序,而是按标签的最后一个字符排序。您通过 s[-1]
获得字符串 s
的最后一个字符。所以,键应该是 lambda t: t[1][-1]
([1] 因为标签在第二个位置;[-1] 因为你想要每个标签的最后一个字符)。
所以:
handles, labels = zip(*sorted(zip(handles, labels), key = lambda t: t[1][-1]))
我想对标签列表进行排序,在标签中字符串的最后一个字符之后从 Matplotlib 中的图例中获得句柄。
到目前为止,我尝试结合这个线程: How is order of items in matplotlib legend determined? 使用此线程: How to sort a list by last character of string 不幸的是,这没有用。这是我的代码:
handles, labels = ax.get_legend_handles_labels()
handles, labels = zip(*sorted(zip(labels, handles), key = lambda t:[0]))
leg = ax.legend(handles, labels,loc='best', ncol=2, shadow=True, fancybox=True)
基本上什么都不做。
打印输出如下:
print(handles)
print(labels)
[<matplotlib.lines.Line2D object at 0x7fd6182ddcd0>, <matplotlib.lines.Line2D object at 0x7fd6182ddb50>, <matplotlib.lines.Line2D object at 0x7fd6448ddc10>, <matplotlib.lines.Line2D object at 0x7fd6448ddf50>, <matplotlib.lines.Line2D object at 0x7fd6609cb790>, <matplotlib.lines.Line2D object at 0x7fd6609cb190>, <matplotlib.lines.Line2D object at 0x7fd660ac5f10>, <matplotlib.lines.Line2D object at 0x7fd660ac5e90>, <matplotlib.lines.Line2D object at 0x7fd619404d10>, <matplotlib.lines.Line2D object at 0x7fd645fcb9d0>]
['Demo_4 maximum', 'Demo_4 mean', 'Demo_5 maximum', 'Demo_5 mean', 'Demo_6 maximum', 'Demo_6 mean', 'Demo_7 maximum', 'Demo_7 mean', 'Demo_8 maximum', 'Demo_8 mean']
你快到了。需要对行进行一些更正
handles, labels = zip(*sorted(zip(labels, handles), key = lambda t:[0]))
如果您希望收到
handles, labels,
,那么您必须按相同的顺序压缩:zip(handles, labels)
按第一个列表排序的键是
lambda t: t[0]
(注意 t)。但是因为我们现在首先有句柄,然后是标签,按标签排序,它将是lambda t: t[1]
(1 是第二个列表,在本例中是标签)但是你不想按标签排序,而是按标签的最后一个字符排序。您通过
s[-1]
获得字符串s
的最后一个字符。所以,键应该是lambda t: t[1][-1]
([1] 因为标签在第二个位置;[-1] 因为你想要每个标签的最后一个字符)。
所以:
handles, labels = zip(*sorted(zip(handles, labels), key = lambda t: t[1][-1]))