matplotlib解決不顯示中文問題

下載微軟雅黑字體

找到字體文件夾

import matplotlib print(matplotlib.matplotlib_fname()) 

以個人爲例子,輸出爲css

C:\Users\adobe\app\anaconda3\lib\site-packages\matplotlib\mpl-data\matplotlibrc

因此說,以我使用的anaconda爲例子,它在lib\site-packages\matplotlib\mpl-data\matplotlibrc文件夾下面.
進入fonts\ttf目錄,把第一步下載的msyh.ttf放到該目錄下面python

 

 

修改matplotlibrc文件

使用任何一個文件編輯器(推薦vscode),修改該文件,經過ctrl+f搜索找到git

#axes.unicode_minus : True ## use unicode for the minus symbol #font.family : sans-serif #font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif 

分別修改成如下三行github

axes.unicode_minus : False ## use unicode for the minus symbol font.family : Microsoft YaHei font.sans-serif : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif 

解釋:緩存

  1. 首先三行都須要刪除第一個#,取消註釋
  2. 第一行,修改True爲False,是爲了正常顯示負號
  3. 第二行和第三行是爲了使用微軟雅黑做爲默認字體

刪除緩存

使用下面的代碼,獲取緩存文件夾.app

import matplotlib print(matplotlib.get_cachedir()) 

個人輸出爲編輯器

C:\Users\adobe\.matplotlib 

因此,通常在用戶目錄的matplotlib,刪除該目錄下的全部文件測試

 
image.png


重啓Python便可(意思是關閉全部正在運行的Python窗口,而後從新打開,要否則沒法生效)字體

 

測試

使用以下的簡單代碼進行測試spa

# coding:utf-8 import matplotlib.pyplot as plt plt.plot((1,2,3),(4,3,-1)) plt.xlabel(u'橫座標') # python3 能夠不用u,已是默認了 plt.ylabel(u'縱座標') plt.show()

相關文章
相關標籤/搜索