Hi:
I’m adjusting the y-position of a plot title. Then, I add a legend with loc set to ‘best’. However, the legend is placed on top of the title.
The following code demonstrates the issue:
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()
Is this a bug in legend, or am I missing something? Any advice on how to place the legend using loc=best without colliding with the title?
rcomer
December 13, 2024, 12:16pm
2
Thank you, @rcomer , for your answer. I just updated Matplotlib to the latest version (3.9.4) and got the same result.
Do you think I should report a bug?
I reported a bug about this issue:
opened 08:25AM - 16 Dec 24 UTC
Documentation: API
### Bug summary
If I adjust the y-position of a plot title to move it down, an… d then I add a legend with loc set to `best`, the legend overlaps with the title.
### Code for reproduction
```Python
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

### 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