當我嘗試使用 matplotlib 繪制圖形時,我用文本和“數學文本”編寫了 x 軸標簽。因為必須在標簽上寫化學式,所以寫成'$CO_2$濃度'。問題是我希望字體應該是新羅馬時代,但我無法以某種方式更改美元符號中的字體。有沒有人可以幫我修復它?非常感謝!import numpy as np import matplotlib.pyplot as pltimport pandas as pdxf1 = pd.read_excel('1812_GPT.xlsx',sheetname= 'PVD_CO2CH4_600',usecols=[1])deltapx1 = pd.read_excel('1812_GPT.xlsx',sheetname= 'PVD_CO2CH4_600',usecols=[3])Px1 = pd.read_excel('1812_GPT.xlsx',sheetname= 'PVD_CO2CH4_600',usecols=[5])ax1 = plt.subplot(111) l1, = ax1.plot(xf1,Px1,'s',markerfacecolor='black')font1 = {'family' : 'Times New Roman','weight' : 'normal','size' : 14, }ax1.set_xlabel(r'$CO_2$ pressure', font1)ax1.set_ylabel(r'$CO_2$ concentration', font1)plt.show()這是圖片鏈接,您可能會看到圖片并發現“CO2”不在 Times new roman 中。 https://flic.kr/p/2dmh8pj
2 回答

MM們
TA貢獻1886條經驗 獲得超2個贊
我發現一次性定義整個字體系列比獨立定義更好(假設您想要相同的字體)。試試這個
plt.rc('text', usetex=True )
plt.rc('font', family='Times New Roman', weight='normal', size=14)
plt.rcParams['mathtext.fontset'] = 'Times New Roman'
ax1.set_xlabel('$CO_2$ pressure')
ax1.set_ylabel('$CO_2$ concentration')
添加回答
舉報
0/150
提交
取消