Matplotlib 2 inconsistent font

前端 未结 3 1064
情书的邮戳
情书的邮戳 2021-01-03 00:39

I updated Anaconda Python to the latest version (4.3), where they upgraded Matplotlib to version 2.

The upgrade has made some major changes to the default style (see

3条回答
  •  萌比男神i
    2021-01-03 00:56

    While trying to find a solution to my question, I tried comparing the dictionaries of the old and new rcParams and setting the elements which were different and related to mathtext font: the result is quite good.

    The code is:

    #%matplotlib inline
    #%matplotlib notebook
    #%config InlineBackend.figure_format = 'svg'
    import scipy as sc
    import matplotlib.pyplot as plt
    import matplotlib
    
    # http://matplotlib.org/users/dflt_style_changes.html
    params = {'legend.fontsize': 18,
              'axes.labelsize': 18,
              'axes.titlesize': 18,
              'xtick.labelsize' :12,
              'ytick.labelsize': 12,
              'mathtext.fontset': 'cm',
              'mathtext.rm': 'serif',
              'mathtext.bf': 'serif:bold',
              'mathtext.it': 'serif:italic',
              'mathtext.sf': 'sans\\-serif',
              'grid.color': 'k',
              'grid.linestyle': ':',
              'grid.linewidth': 0.5,
             }
    matplotlib.rcParams.update(params)
    #matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath, newtxmath}']})
    #matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath, mathptmx}']})
    #matplotlib.rcParams.update({'text.usetex':True, 'text.latex.preamble':[r'\usepackage{amsmath}']})
    
    x = sc.linspace(0,100)
    y = x**2
    fig = plt.figure('Fig')
    ax = fig.add_subplot(1, 1, 1)
    lines = ax.semilogy(x, y)
    ax.set_yticks([300], minor=True)
    ax.yaxis.grid(True, which='minor')
    ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
    ax.tick_params(axis='y', pad=10)
    ax.set_xlabel(r'$\mathrm{R_L}$')
    ax.set_ylabel(r'$\sigma \int_l \; dx$')
    fig.savefig('./PNG/test.png', dpi=300, bbox_inches='tight')
    

    hence adding also:

              'mathtext.rm': 'serif',
              'mathtext.bf': 'serif:bold',
              'mathtext.it': 'serif:italic',
              'mathtext.sf': 'sans\\-serif',
    

    which results in:

    that I consider quite good and consistent in a Latex document.

    The other answer in this thread from @ImportanceOfBeingErnest is also neat and nice.

提交回复
热议问题