0

In a word, I'd like to know the width and height of a legend so that my program could modify the axes automatically.

Here is a image that illustrated my question:

image

As you can see, some part of the legend goes out of the axes. I think this problem could be solved if I know the height and width of the legend.

I have readed Rectangle, FancyBboxPatch and something that will helpfull, I think, on the official site. I know that there is something about size: For FancyBboxPatch:

get_frame():
  Return the Rectangle used to frame the legend. But it returns a FancyBboxPatch in fact.

get_bbox_to_anchor():
  Return the bbox that the legend will be anchored to. But I know that the function has nothing to do with my problem.

I'd like to know the height and width of the grey border that a arrow pointed. An arrow pointed the border:

image

Thank you for your help!

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
  • 1
    Does this answer your question? [How to get legend location in matplotlib](https://stackoverflow.com/questions/43747614/how-to-get-legend-location-in-matplotlib) – Jody Klymak Aug 07 '22 at 21:48

1 Answers1

0

If the problem is that the legend is overlapping with the data, then one option is to move the legend outside the plot with bbox_to_anchor with the below as shown in this answer:

plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")

Or you could try to reduce the font size with:

plt.legend(fontsize=6, loc="upper right")
viggnah
  • 1,709
  • 1
  • 3
  • 12
  • Thank you for help. But I'd like to plot the legend inside. I'd like to program for modifing the figure size to avoid overlapping automaticlly. – Afeather2017 Aug 08 '22 at 14:03