matplotlib: alignment of legend title

后端 未结 2 1336
南方客
南方客 2021-02-02 10:27

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

相关标签:
2条回答
  • 2021-02-02 10:32

    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
    
    0 讨论(0)
  • 2021-02-02 10:47

    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"
      

    0 讨论(0)
提交回复
热议问题