Matplotlib: label tags do not match font settings (LaTeX example text)

I have the following simple python code:

import numpy as np
import matplotlib.pyplot as plt 

plt.rc( 'font', size=20, family="Times" )   # use a font with serifs

# the following line triggers the problem
plt.rc( 'text', usetex=True )               # activate LaTeX text rendering

fig = plt.figure( figsize=(8,6) )           # (width,height) in inches
ax1 = fig.add_subplot( 1, 1, 1 )            # rows cols plotnumber

ax1.plot( np.linspace(1,10,10), np.linspace(1,10,10)**2 )

ax1.set_xlabel( r'\textit{x} in a.u.' )
ax1.set_ylabel( r'\textit{y} in a.u.' )

plt.show()

This leads to the following figure: Font thickness in labels is incorrect

As you can see, the label labels are too thin in comparison to the label axes (or label labels are too thick). I found out that this is due to the activation of LaTeX text conversion (see the comment in the code), but I don’t know how to change this, because I don’t want to disable LaTeX text rendering.

Any idea why the font thickness (what is the plural of the thickness?) Is inconsistent and how to change this?

Update 1 . Following the suggestion llap42 , will hack

plt.xticks([2, 4, 6, 8, 10], ['2', '4', '8', '10' ])

But this is only a hack and should be the best solution.

+4
2

, , .

ScalarFormatter ( ). GitHub.

Formatter. , StrMethodFormatter:

import matplotlib.pyplot as plt 
import matplotlib.ticker

plt.rc( 'text', usetex=True ) 
plt.rc('font',family = 'sans-serif',  size=20)

fig , ax = plt.subplots(figsize=(5,3))

ax.set_xlabel( r'\textit{x} in a.u.' )
ax.set_ylabel( r'\textit{y} in a.u.' )

fmt = matplotlib.ticker.StrMethodFormatter("{x}")
ax.xaxis.set_major_formatter(fmt)
ax.yaxis.set_major_formatter(fmt)

plt.tight_layout()
plt.show()

enter image description here

+2

, . , tex.stackexchange.

sfmath. ,

    plt.rc('text.latex', preamble=r'\usepackage[cm]{sfmath}')

, .

0

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


All Articles