Matplotlib 2 inconsistent font

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

The update introduced some significant changes to the default style ( see here ). And, although I really like some of these changes, I disagree with some of them.

Therefore, I made some changes, as indicated in the link above:

#%matplotlib inline #%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', 'grid.color': 'k', 'grid.linestyle': ':', 'grid.linewidth': 0.5, } 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.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') 

Using latex as axis labels, as in the above code, leads to a figure with inconsistent text along the axes (see the following image).

enter image description here

How to return to the previous behavior (see image below) or to a consistent font layout?

enter image description here

EDIT : Using the Latex back-end, I can get a good result, but it is very slow. In any case, I think that the inner part should be able to get consistent output, and switching to another interface is not a real solution, but a more workaround.

To use the latex end:

 #%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', '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}']}) 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') 

Result with matplotlib 2:

enter image description here

The resulting plot with an older version (still a bit different, maybe due to some latex differences):

enter image description here

But then again, the desired result is what is obtained from an older version of matplotlib and shown in Figure 2.

+6
source share
3 answers

When I tried to find a solution to my question, I tried to compare the dictionaries of the old and new rcParams and install elements that were different and related to the mathtext font: the result is pretty good.

The code:

 #%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') 

therefore adding also:

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

that leads to:

enter image description here

which I find pretty good and consistent in the latex document.

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

0
source

If consistency is the only issue, you can use the Roman style using the Times font. No need to use latex via usetex . Instead, just use the STIX font, 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() 

enter image description here

+4
source

From the link you provided:

A 'classic stylesheet is provided, so returning to the default values โ€‹โ€‹of 1.x is one python line

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 was returning matplotlib 1.x fonts).

+1
source

Source: https://habr.com/ru/post/1014991/


All Articles