Vertical alignment of matplotlib legend labels with LaTeX math

后端 未结 2 1778
梦谈多话
梦谈多话 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:05

    Set the text.latex.preview parameter to True:

    import numpy as np
    import matplotlib as mpl
    mpl.rcParams['text.usetex'] = True
    mpl.rcParams['text.latex.preview'] = True
    import matplotlib.pyplot as plt
    
    x = np.arange(10)
    plt.plot(x, np.random.uniform(size=(10,)), c='red', label=r'test')
    plt.scatter(x, np.random.uniform(size=(10,)), c='blue', label=r'test${}_{xy}$')
    plt.legend(ncol=2)                                                      
    
    plt.show()
    

    For the effect of the preview argument, also refer to this example.

提交回复
热议问题