Home > Mobile >  How to group legend handles in matplotlib?
How to group legend handles in matplotlib?

Time:03-17

I was wondering how I can produce such fancy legends in matplotlib. In particular, I'd like to know how to group, for example, both solid and dashed blue lines which correspond to $\tau=10$ side by side, or all dashed (or solid) lines together as in the lower part of the legend.

enter image description here

The image is taken from enter image description here


Take-home messages

  1. The legend docs specifies a more natural way to group the entries using HandlerTuple (example here). But since mpl places the markers horizontally, such an approach is orthogonal to what I wished :). If it doesn't bother you, go for that option first.

  2. As far as I understood, custom legends are designed such that they don't exchange any information with the data you want to plots. For example, in my case, I cannot tell the AnyObjectHandler how many lines are going to be grouped, what are their linestyle, etc. It is a good decision for generality, with the expense of a (minimal) harm to the code refactoring.

  • Related