Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Legend with location set to ‘best’ overlaps with the title when the titles is moved down #29319

Open
aweinstein opened this issue Dec 16, 2024 · 6 comments
Labels
Documentation: API files in lib/ and doc/api

Comments

@aweinstein
Copy link

Bug summary

If I adjust the y-position of a plot title to move it down, and then I add a legend with loc set to best, the legend overlaps with the title.

Code for reproduction

import matplotlib.pyplot as plt

plt.close('all')
plt.plot((1,2,3), label='Just a very long legend')
plt.title('Just a very long title 1234567890', y=0.9)
plt.legend(loc='best')
plt.show()

Actual outcome

foo

Expected outcome

I expect the legend to be placed in a location that does not overlap with the title.

Additional information

No response

Operating system

Linux

Matplotlib Version

3.9.4

Matplotlib Backend

qtagg

Python version

3.12.7

Jupyter version

No response

Installation

pip

@timhoffm
Copy link
Member

I'm inclined to close as "won't fix". Titles are not considered data content and thus not taken into account for legend placement. The same would hold true for axis labels.

If you want to place text inside the Axes, I propose to use plt.text() instead, e.g.

plt.text(0.5, 0.9, 'Just a very long title 1234567890', ha="center", transform=plt.gca().transAxes)

@Shivangbijalwan
Copy link

Shivangbijalwan commented Dec 16, 2024

we can do many things for this issue 1 is that we can position the legend outside the fox here is the code for this I have used
{ bbox_to_anchor } for this

`
import matplotlib.pyplot as plt

plt.close('all')
plt.plot((1, 2, 3), label='Just a very long legend')
plt.title('Just a very long title 1234567890', y=0.9)
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), ncol=1)
plt.show()

`

@jklymak
Copy link
Member

jklymak commented Dec 16, 2024

Agree with @timhoffm that this is likely a won't fix. If the user wants to place the title in such a non-default location they can also place the legend manually to use a bbox as suggested by @Shivangbijalwan

@aweinstein
Copy link
Author

Thank you all for your answers. My use case is inside a 2-by-4 subplot, where each subplot has a different legend. For this reason, placing the legend outside the axes is not an option in this case. I'll try using plt.text as suggested by @timhoffm .

Given that y is a documented parameter of plt.title, shouldn't the documentation mention that playing with it won't guarantee that plt.leged(loc='best') will do its job?

@jklymak
Copy link
Member

jklymak commented Dec 16, 2024

Given that y is a documented parameter of plt.title, shouldn't the documentation mention that playing with it won't guarantee that plt.leged(loc='best') will do its job?

I'd go the other way, and in legend document that best only works for artists added to the axes, not axes labels.

@timhoffm timhoffm added the Documentation: API files in lib/ and doc/api label Dec 17, 2024
@timhoffm
Copy link
Member

I agree that more detailed documentation of the loc="best" is the way to improve here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: API files in lib/ and doc/api
Projects
None yet
Development

No branches or pull requests

4 participants