如何设置标题、轴的matplotlib字体,

2024-04-24 06:08:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道怎么改字体,比如说“时代”。我的尝试没有成功。在

import numpy as np
import matplotlib.pyplot as plt

x     = np.array([0, 75, 150])
y     = np.array([0, 1, 3])
coeff = np.polyfit(x, y, 2)

xx = np.linspace(0, 150, 150)
yy = coeff[0] * xx**2 + coeff[1] * xx + coeff[2]

plt.title(unicode("Correction factor", "utf-8"),fontname="times")
plt.xlabel(unicode("Temperature in °C", "utf-8"))
plt.ylabel(unicode("fKorr", "utf-8"))
plt.plot(xx,yy)
plt.show()

编辑:与“时代新罗马”一起使用。我用的其他程序知道“时间”。在


Tags: importnumpymatplotlibasnpunicode字体plt
1条回答
网友
1楼 · 发布于 2024-04-24 06:08:54

根据平台的不同,Times New Roman字体的名称可能不同,例如在我当前的机器中,Mac OS

import matplotlib.font_manager as fmg
for i, item in enumerate(fmg.findSystemFonts()):
    try:
        name=fmg.FontProperties(fname=item).get_name()
        if 'TIMES' in name.upper():
            print name, item, i
    except:
        pass

#Times New Roman /Library/Fonts/Times New Roman Italic.ttf 223
#Times New Roman /Library/Fonts/Times New Roman Bold Italic.ttf 232
#Times New Roman /Library/Fonts/Times New Roman Bold.ttf 336
#Times New Roman /Library/Fonts/Times New Roman.ttf 367

我认为这个解决方案可能更安全:直接提供字体文件的路径。matplotlib.font_manager.findSystemFonts()将列出每个文件的路径:

^{pr2}$

enter image description here

相关问题 更多 >