In matplotlib, how can I adjust the alignment of the legend title? It is always centered, but I need it to be left aligned with the legend box. I tried to change the al
I think you need to displace the Text
object, using the set_position((x, y))
method. The units of x
and y
are pixels, so you'll have to experiment with what values look right, or use a Transform
. I'm not sure off hand which combination of Transforms
might be most useful.
So in short, something like this might work:
l.get_title().set_position((-10, 0)) # -10 is a guess
You may align the complete legend box setting leg._legend_box.align
. This aligns everything inside the legend box, but the effect is the desired one to have the title on either side of the box instead of the center.
Left aligned
leg = plt.legend(title="Title")
leg._legend_box.align = "left"
Right aligned
leg = plt.legend(title="Title")
leg._legend_box.align = "right"