How is order of items in matplotlib legend determined?

后端 未结 5 765
迷失自我
迷失自我 2020-12-12 17:43

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,         


        
5条回答
  •  轮回少年
    2020-12-12 18:20

    A slight variation on some other aswers. The list order should have the same length as the number of legend items, and specifies the new order manually.

    handles, labels = plt.gca().get_legend_handles_labels()
    order = [0,2,1]
    plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order])
    

提交回复
热议问题