Matplotlib 2 inconsistent font

前端 未结 3 1042
情书的邮戳
情书的邮戳 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-03 01:10

    From the link you did provide:

    A ‘classic’ style sheet is provided so reverting to the 1.x default values is a single line of python

    mpl.style.use('classic')

    Adding this line

    matplotlib.style.use('classic')
    

    to your script should solve your problem.

    I tested it on my python2.7/matplotlib 2, and it worked fine (i.e. I get back the matplotlib 1.x fonts).

    0 讨论(0)
  • 2021-01-03 01:11

    If consistency is the only issue, you can use a "Roman" style using the "Times" font. It is not necessary to use Latex via usetex. Instead simply use the STIX fontset, the Times font and serif mathtext.

    import scipy as sc
    import matplotlib.style
    import matplotlib.pyplot as plt
    
    params = {'legend.fontsize': 18,
              'axes.labelsize': 18,
              'axes.titlesize': 18,
              'xtick.labelsize' :12,
              'ytick.labelsize': 12,
              'grid.color': 'k',
              'grid.linestyle': ':',
              'grid.linewidth': 0.5,
              'mathtext.fontset' : 'stix',
              'mathtext.rm'      : 'serif',
              'font.family'      : 'serif',
              'font.serif'       : "Times New Roman", # or "Times"          
             }
    matplotlib.rcParams.update(params)
    
    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.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
    ax.tick_params(axis='y', pad=10)
    ax.set_yticks([300], minor=True)
    ax.yaxis.grid(True, which='minor')
    ax.set_xlabel(r'$\mathrm{R_L}$')
    ax.set_ylabel(r'$\sigma \int_l \; dx$')
    plt.tight_layout()
    plt.show()
    

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