Upright mu in plot label: retaining original tick fonts

后端 未结 3 1235
萌比男神i
萌比男神i 2021-01-05 22:30

I have a problem which I thought would be more occurring. However, after scouring the internet for some time now I have not been able to find the solution to my problem. So

3条回答
  •  难免孤独
    2021-01-05 23:16

    I am also struggling with such a problem, i.e. getting the tick labels and axes labels to be consistent when text.usetex = True. The solution that I have managed to find it not ideal, but it works for the moment.

    What you have to do is set the font family to "sans-serif" and also add a latex package that uses sans-serif math fonts (sfmath -- make sure it is in your tex path!)

    import matplotlib
    import matplotlib.pyplot as plt
    
    matplotlib.rc('text', usetex = True)
    matplotlib.rc('font', **{'family':"sans-serif"})
    params = {'text.latex.preamble': [r'\usepackage{siunitx}', 
        r'\usepackage{sfmath}', r'\sisetup{detect-family = true}',
        r'\usepackage{amsmath}']}   
    plt.rcParams.update(params)   
    
    fig = plt.figure(figsize = (4,4))
    ax = fig.add_subplot(1,1,1)
    ax.set_xlabel('$\si{\um} detection$')
    ax.set_ylabel(r"$\mu \boldsymbol{\mu}$")
    plt.show()                              
    

    In addition, I had to tell the siunitx package to detect the font family, and I also had to add some additional text to the x-label in order for the detection to actually work (you can remove this text later and the label will still work after that).

    For me this results in: enter image description here More generally, I have the following my ~/.matplotlib/matploblibrc file, for serif fonts:

    font.family    : serif 
    text.latex.preamble    :   \usepackage{mathptmx}
    

    and for sans-serif:

    font.family : sans-serif 
    text.latex.preamble :   \usepackage{sfmath}
    

提交回复
热议问题