Vertical alignment of matplotlib legend labels with LaTeX math

后端 未结 2 1779
梦谈多话
梦谈多话 2021-01-05 12:53

When mixing labels that have subscripts with labels without them, they do not vertically align properly in the legend. Since matplotlib determines bounding boxes internally

2条回答
  •  无人及你
    2021-01-05 13:22

    You can have a look on Text alignment in a Matplotlib legend.

    Or you can just shift down the second legend text,

    h_legend = plt.legend(ncol=2)
    y_shift = -2.5
    h_legend.texts[1].set_position((0, y_shift))
    

    You can peak your shift distance based on the extent of the legend window using something like:

    h_legend = plt.legend(ncol=2)    
    renderer = plt.gcf().canvas.get_renderer()
    y_shift = -0.2*h_legend.texts[0].get_window_extent(renderer).height
    h_legend.texts[1].set_position((0, y_shift))
    

    this will shift the second text by 20% of the full legend window height.

提交回复
热议问题