0

I plotted a figure as the attached picture. There are 3 labels in the legend. As you can see, the label on the second row is quite long, so I'd like to set the right of “BS” label align with the end of "Farinotti et al. (2019)" label. How should I do this?

enter image description here

keeptg
  • 27
  • 6

1 Answers1

2

Basically, you're thinking "these labels form a 2x2 grid, and I want the bottom row to span 2 columns"?

To be honest, I don't think that's necessary; to my eyes your legend looks fine. This won't save as much white space, but you could save some by just swapping the positions of "BS" and "Farinotti". Saving more white space than that would be inconsequential here. You can have a look at this other answer for tips on how to flip the label positions:

Matplotlib legend, add items across columns instead of down

But if you want to go down the path of creating column-spanning labels anyway, for a more extreme case or out of curiosity, what you want to do is define your own legend handler. Required reading:

https://matplotlib.org/1.3.1/users/legend_guide.html

https://matplotlib.org/3.1.0/api/legend_handler_api.html

Or here's a somewhat hacky answer I found that combines 2 legends into 1, which should give you the effect that you're looking for.

pyplot: change ncols within a legend

Michael
  • 2,344
  • 6
  • 12