【发布时间】:2021-02-25 12:59:12
【问题描述】:
我想在标签中字符串的最后一个字符之后对从 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']
【问题讨论】:
标签: sorting matplotlib zip legend