【问题标题】:How to prevent change of font family in Matplotlib?如何防止在 Matplotlib 中更改字体系列?
【发布时间】:2020-01-02 10:17:48
【问题描述】:

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

Please click here for an example figure

可以看出,f中的x和legend中的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()

这个问题有解决办法吗?

【问题讨论】:

    标签: matplotlib fonts python-3.7


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2014-02-14
      • 2019-01-10
      相关资源
      最近更新 更多