Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make multiline title of matplotlib legend centered

I have a plot with a legend. Its title has multiple lines like so

plot with legend

created by this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot()
legend = ax.legend(title="Line 1 is short\nLine 2 is slightly longer")
plt.show()

I would like the individual lines of the legend title to be centered. How do I accomplish this?

>Solution :

Adding this will do the job:

legend.get_title().set_multialignment('center')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading