天天看點

python matplotlib畫圖中文顯示

matplotlib 中文顯示

    • 1. 檢視字型路徑
    • 2. 删除matplotlib的緩沖目錄
    • 3. 修改matplotlibrc檔案
    • 4. 最後使用jupyterlab的話,重新開機

1. 檢視字型路徑

import matplotlib    
print(matplotlib.matplotlib_fname())
           
python matplotlib畫圖中文顯示

網上給的中文字型連結,但是沒下載下傳下來,從其他地方找的

https://www.fontpalace.com/font-download/SimHei/

把 ttf 格式的中文字型檔案放到

path_to/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf
           
python matplotlib畫圖中文顯示

2. 删除matplotlib的緩沖目錄

import matplotlib
matplotlib.get_cachedir()
           
python matplotlib畫圖中文顯示

删除緩存

3. 修改matplotlibrc檔案

import matplotlib
print(matplotlib.matplotlib_fname())
           
python matplotlib畫圖中文顯示
python matplotlib畫圖中文顯示
python matplotlib畫圖中文顯示

4. 最後使用jupyterlab的話,重新開機

python matplotlib畫圖中文顯示

顯示

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.plot([1,2,3,4],[1,2,3,4])
plt.title("中文标題")
plt.show()
           
python matplotlib畫圖中文顯示

檢視已安裝字型

from matplotlib.font_manager import FontManager
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print(mat_fonts)
           

繼續閱讀