How is order of items in matplotlib legend determined?

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])

Leave a Comment