簡體   English   中英

使Matplotlib在數學模式下顯示襯線字體

[英]Make matplotlib display serif font in math mode

在使用matplotlib進行數學運算時,如何獲得字體為Times New Roman的字體。 默認字體為斜體,我正在嘗試使其與其他坐標軸標題匹配。

我正在嘗試使用\\ mathrm {}:

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

就像在matplotlib網站( https://matplotlib.org/users/mathtext.html )上所建議的一樣,但它仍然以無襯線字體顯示。 有什么方法可以使我成為時代新羅馬人嗎? 謝謝!

解釋您要在軸上的文本以及繪圖中使用的任何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()

在此處輸入圖片說明

另一個用例是希望在數學模式下使用襯線字體,但僅適用於特定字符。 一種解決方案是將fontset設置為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}$)')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM