-1

For years I have been wondering why the numbering of Python legend location makes no sense:

Location String Location Code
'best'          0
'upper right'   1
'upper left'    2
'lower left'    3
'lower right'   4
'right'         5
'center left'   6
'center right'  7
'lower center'  8
'upper center'  9
'center'        10

For me it would make much more sense to attribute the numbers as if they were on a cellphone:

Location String Location Code
'best'          0
'upper left'    1
'upper center'  2
'upper right'   3
'center left'   4
'center'        5
'center right'  6
'lower left'    7
'lower center'  8
'lower right'   9

Is there any way I can manipulate this in a script, which I can call every time I do plotting so I don't have to go tho the matplotlib site every time?

Or is there any change they will be changing it soon?

ps: What is the purpose of having both 'right' and 'center right' as legend location options?

Bollehenk
  • 285
  • 2
  • 19
  • 2
    Changing it now would cause a huge amount of backward breaking incompatibility... Why do the numbers matter so much to you anyway - why not just use the names? – Jon Clements Oct 30 '18 at 08:27
  • @JonClements because I can't remember the names exactly. – Bollehenk Oct 30 '18 at 08:28
  • If the numbers would follow other standards, like the numbers on your telephone, or on your numberpad on the keyboard, then yes, I would easily remember. With the names I an always doubting: "Is it 'top left' or 'upper left'?" Doing some basic plotting every several months or so I find myself forgetting the exact names of the legend location every time. Honestly, I can't be the only one having the same problem right?! – Bollehenk Oct 30 '18 at 08:32

2 Answers2

2

The numbers are essentially there for backward compatibility. Most examples on the matplotlib documentation have already been changed to use the text form.
For the text you only need to remember that

  • the vertical direction comes first ("upper", "center", "lower") followed by the horizontal direction ("left", "center", right")
  • There is no "center center", and that is simply "center".


You may of course print out this little picture [*]

enter image description here

and pin it to your screen.


A more consise way would be to use a "compass" notation like "N", "NW", "SE" etc. Which was already suggested here. I now proposed such solution in this pull request.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
2

If you're desperate, you can do something like

locations = [0, 2, 9, 1, 6, 10, 7, 3, 8, 4]

plt.legend(loc=locations[telephone_index])

(and if you're really desperate, you could even write a wrapper for legend()).

cheersmate
  • 2,385
  • 4
  • 19
  • 32
  • 1
    Me neither, but lots of code is only ever touched and used by a single person. That's a playground where anything is allowed. – cheersmate Oct 30 '18 at 08:37