Linux中matplotlib 中文顯示問題解決


1.下載下載中文 arial unicode ms 字體到 /home 目錄

2. 拷貝字體到 usr/share/fonts 下:

  • sudo cp ~/arial\ unicode\ ms.ttf /usr/share/fonts/arial\ unicode\ ms.ttf

3. 修改配置文件,運行下列代碼查看配置文件位置

  • import matplotlib print(matplotlib.matplotlib_fname()) #輸出配置文件位置 # eg:/home/python/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

進入到上述找到的配置文件中,修改下面兩項

sudo vim /home/python/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
# 打開這一行注釋
font.family         : sans-serif # 在原有字體之前,新增我們剛下載的中文字體 arial unicode ms
font.sans-serif     : arial unicode ms, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

 4. 這一步驟最重要!為matplotlib增加中文字體.將下載的arial unicode ms.ttf字體文件復制一份到步驟2找到的配置文件的同級文件夾fonts中:

sudo cp ~/arial\ unicode\ ms.ttf /home/python/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/arial\ unicode\ ms.ttf

 5. 清除本地matplotlib中的緩存

  • rm -rf ~/.cache/matplotlib

現在就可以重新畫圖,查看中文效果了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM