Legend Matplotlib: Marker Inscriptions

I have a graph with several lines, each of which is marked separately. I would like to put a legend in the plot so that individual lines can be identified. The default ordering of labels and markers looks something like this:

marker: shortcut
marker: shortcut
marker: shortcut
... etc.

For various aesthetic reasons, I would like the number of columns in my legend to equal the number of rows (and labels) that I change with the ncol option. So, currently my legend looks like this:

marker: label marker: label marker: label

It would be much clearer if I had marks above the markers in this layout. I would like something like:

label label
marker marker marker

I am wondering if there is a β€œquick fix” way to achieve such a legend.

+5
source share
1 answer

In the end, as I did, it was to manually adjust the position of the text elements in the legend, using legend.get_texts() to iterate over each text object. Some dummy codes:

 for txt in legend.get_texts(): txt.set_ha("center") # horizontal alignment of text item txt.set_x(-5) # x-position txt.set_y(10) # y-position 

By increasing the y position, you push the label up (i.e. above the marker). Similarly, by decreasing the x position, alignment on the top of the marker can be achieved.

+1
source

Source: https://habr.com/ru/post/1203805/


All Articles