Python 使matplotlib在数学模式下显示衬线字体

Python 使matplotlib在数学模式下显示衬线字体,python,matplotlib,Python,Matplotlib,在matplotlib中使用数学键入时,如何将字体设置为Times New Roman。默认字体为斜体,我正在尝试使其与其他轴标题匹配 我正在尝试使用\mathrm{}: plt.xlabel(r"$\frac{\mathrm{1}}{{\mathrm{Distance}}^2 \ (\mathrm{m}^2)}$") 正如matplotlib网站()上所建议的,但它仍然显示为无衬线字体。有没有办法把它变成新罗马时代?谢谢 解释您要在轴上的文本以及绘图上使用的任何MathText中查找衬线字体

在matplotlib中使用数学键入时,如何将字体设置为Times New Roman。默认字体为斜体,我正在尝试使其与其他轴标题匹配

我正在尝试使用\mathrm{}:

plt.xlabel(r"$\frac{\mathrm{1}}{{\mathrm{Distance}}^2 \ (\mathrm{m}^2)}$")

正如matplotlib网站()上所建议的,但它仍然显示为无衬线字体。有没有办法把它变成新罗马时代?谢谢

解释您要在轴上的文本以及绘图上使用的任何MathText中查找衬线字体的问题

import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "serif"
plt.rcParams["mathtext.fontset"] = "dejavuserif"

plt.xlabel(r"$\frac{\mathrm{1}}{{\mathrm{Distance}}^2 \ (\mathrm{m}^2)}$")
plt.ylabel("Some normal text")
plt.tight_layout()
plt.show()

另一个用例是希望在数学模式下使用衬线字体,但仅限于特定字符。A是将字体集设置为
自定义
,然后仅为
数学
(书法)字符集设置衬线字体。在这里,我还将字体样式设置为斜体:

import matplotlib.pyplot as plt

plt.rcParams['mathtext.fontset'] = 'custom'
plt.rcParams['mathtext.cal'] = 'stix:italic'  # changes only the mathcal subfamily
然后,获取一个衬线字符(此处为d),但将其余字符保留为默认的数学字体:

plt.ylabel(r'Label ($a^b = \mathcal{d}$)')

这里有些东西混在一起。您是在谈论字体本身(例如Arial、Helvetica、Times New Roman)还是字体样式(例如normal、italic、bold)?非常感谢!对不起,如果我的问题不是很清楚,但这很好:)