【问题标题】:Make matplotlib display serif font in math mode使matplotlib在数学模式下显示衬线字体
【发布时间】:2018-02-06 15:38:09
【问题描述】:

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

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

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

按照 matplotlib 网站 (https://matplotlib.org/users/mathtext.html) 的建议,但它仍然显示为无衬线字体。有没有办法把它变成Times new Roman?谢谢!

【问题讨论】:

  • 这里混杂了一些东西。您是在谈论字体本身(例如 Arial、Helvetica、Times New Roman)还是字体样式(例如正常、斜体、粗体)?

标签: python matplotlib


【解决方案1】:

解释您在轴上的文本以及绘图上使用的任何 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()

【讨论】:

  • 非常感谢!抱歉,如果我的问题不是很清楚,但效果很好:)
【解决方案2】:

另一个用例是在数学模式下需要衬线字体,但仅限于特定字符。 solution 是将字体集设置为custom,然后仅为mathcal(书法)字符集设置衬线字体。在这里,我还将字体样式设置为斜体:

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}$)')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-24
    • 2014-01-09
    • 2014-01-10
    • 2019-02-18
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多