簡體   English   中英

如何為標題,軸,

[英]How to set matplotlib font for title, axes,

我不知道如何更改字體,比方說“時代”。 我的嘗試無效。

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()

編輯:它與“ Times New Roman”一起使用。 我使用的其他程序也稱為“時間”。

取決於平台,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()將列出每個文件的路徑:

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]

all_font = fmg.findSystemFonts()
fontprop = fmg.FontProperties(fname=all_font[223]) #you may have a different index
plt.title(unicode("Correction factor", "utf-8"), fontproperties=fontprop)
fontprop = fmg.FontProperties(fname=all_font[232])
plt.xlabel(unicode("Temperature in °C", "utf-8"), fontproperties=fontprop)
fontprop = fmg.FontProperties(fname=all_font[236])
plt.ylabel(unicode("fKorr", "utf-8"), fontproperties=fontprop)
plt.plot(xx,yy)

在此處輸入圖片說明

暫無
暫無

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

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