如何防止 Matplotlib 中字体系列的更改?

How to prevent change of font family in Matplotlib?

我编写了一个简单的代码来绘制两个函数 - f(x) 和 g(x)。我的字体有问题。我已将 "Times New Roman" 定义为全局字体,但如果我尝试使用斜体样式 - $$,字体会发生变化。我希望函数 f(x) 和 g(x) 中的变量 x 为斜体,并且在 "Times New Roman".

Please click here for an example figure

可以看出f中的x和图例中的g都是斜体,但不是"Times New Roman"字体

这是我的代码,我正在使用 python 3.7.

import scipy as sy
from matplotlib import  pyplot as plt

x = sy.linspace(0, 4.188, 100)

y1 = -sy.sqrt(x) - x*sy.sqrt(x) + 0.4*x**2
y2 = - sy.exp(0.25*x) + x**0.6

plt.rcParams['font.family']='Times New Roman'

fig_1 = plt.figure(1, figsize=(1.25*6.4,1.25*4.8))

chart_1 = fig_1.add_subplot(1,1,1)

chart_1.plot(x,y1, '-k', linewidth = 2, label = 'f($x$)')
chart_1.plot(x,y2, '-b', linewidth = 2, label = 'g($x$)')

chart_1.tick_params(direction='in', size=8, width=1.0, labelsize=20)

chart_1.set_xlabel('time, s', fontsize = 24)
chart_1.set_ylabel('Force, N', fontsize = 24)

chart_1.set_xlim(0,5)
chart_1.set_ylim(-4,0)

chart_1.xaxis.set_ticks_position('both')
chart_1.yaxis.set_ticks_position('both')

chart_1.legend(fontsize = '20',bbox_to_anchor=(0.85,.7), edgecolor='k', borderpad = .25)

plt.show()

这个问题有解决办法吗?

美元符号 $...$ 之间的文本不是斜体,它是发送到 mathtext 进行处理的文本。

根据上面的 link,mathtext 有它自己的 rc 参数和更改所用字体的方法,包括使用 $\mathdefault{...}$ 来匹配常规文本。但是,如果您不想使用无法在普通文本中书写的符号和方程式,则最好完全不使用 mathtext。