matplotlib: put legend symbols on the right of the

2020-02-26 08:59发布

It's a simple thing but I've searched for quite a while without success: I want to customise a figure legend by reversing the horizontal order of the symbols and labels.

In Gnuplot, this is simply achieved by set key reverse. Example: change x data1 to data1 x. In matplotlib, there seems to be no user-friendly solution. Thus, I thought about changing a kind of handle anchor or just shifting the handle's position, but couldn't find any point to start with.

1条回答
小情绪 Triste *
2楼-- · 2020-02-26 09:53

The requested feature is already there, as the keyword markerfirst of the legend command.

    plt.plot([1,2],[3,4], label='labeltext')
    plt.legend(markerfirst=False)
    plt.show()

If you want to make it your default behaviour, you can change the value of legend.markerfirst in rcParams, see customizing matplotlib.

查看更多
登录 后发表回答