Upright mu in plot label: retaining original tick fonts

后端 未结 3 1236
萌比男神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}
    
    0 讨论(0)
  • 2021-01-05 23:23

    What worked for me was not to usetex, but to use Unicode:

    ax.set_xlabel(u'\u03bc')
    

    sets the label as a single upright mu.

    This requires the following settings when loading matplotlib:

    import matplotlib
    matplotlib.rcParams['mathtext.fontset'] = 'cm'
    matplotlib.rc('font', family='serif', serif='CMU Serif')
    import matplotlib.pyplot as plt
    

    Here I'm using the "Computer Modern Unicode" font from Sourceforge (highly recommended if you'd like consistency with writing typeset in LaTeX and its default Computer Modern font).

    But any unicode font with the mu glyph should work. Actually, the mu from CMU Serif is not as aesthetically nice as the mu from SIunitx, but it is correct.

    Python needed to be restarted for that to take effect.

    0 讨论(0)
  • 2021-01-05 23:24

    I had the same problem and this solved it:

    In your matplotlibrc file change

    mathtext.default : it
    

    to

    mathtext.default : regular
    
    0 讨论(0)
提交回复
热议问题