Matplotlib, Consistent font using latex

后端 未结 2 1132
后悔当初
后悔当初 2020-12-02 07:50

My problem is I\'d like to use Latex titles in some plots, and no latex in others. Right now, matplotlib has two different default fonts for Latex titles and non-Latex titl

相关标签:
2条回答
  • 2020-12-02 08:36

    EDIT

    if you want to change the fonts used by LaTeX inside matplotlib, check out this page

    http://matplotlib.sourceforge.net/users/usetex.html

    one of the examples there is

    from matplotlib import rc
    rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
    ## for Palatino and other serif fonts use:
    #rc('font',**{'family':'serif','serif':['Palatino']})
    rc('text', usetex=True)
    

    Just pick your favorite!

    And if you want a bold font, you can try \mathbf

    plt.title(r'$\mathbf{W_y(\tau, j=3)}$')
    

    EDIT 2

    The following will make bold font default for you

    font = {'family' : 'monospace',
            'weight' : 'bold',
            'size'   : 22}
    
    rc('font', **font)
    
    0 讨论(0)
  • 2020-12-02 08:40

    To make the tex-style/mathtext text look like the regular text, you need to set the mathtext font to Bitstream Vera Sans,

    import matplotlib
    matplotlib.rcParams['mathtext.fontset'] = 'custom'
    matplotlib.rcParams['mathtext.rm'] = 'Bitstream Vera Sans'
    matplotlib.rcParams['mathtext.it'] = 'Bitstream Vera Sans:italic'
    matplotlib.rcParams['mathtext.bf'] = 'Bitstream Vera Sans:bold'
    matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')
    

    If you want the regular text to look like the mathtext text, you can change everything to Stix. This will affect labels, titles, ticks, etc.

    import matplotlib
    matplotlib.rcParams['mathtext.fontset'] = 'stix'
    matplotlib.rcParams['font.family'] = 'STIXGeneral'
    matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')
    

    Basic idea is that you need to set both the regular and mathtext fonts to be the same, and the method of doing so is a bit obscure. You can see a list of the custom fonts,

    sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
    

    As others mentioned, you can also have Latex render everything for you with one font by setting text.usetex in the rcParams, but that's slow and not entirely necessary.

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