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:

import matplotlib.pyplot as plt

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

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:


Leave a Reply