linux亂碼問題解決

一、解決Linux亂碼問題

  1. 問題出現在運行matplotlib圖片的title亂碼
    2.步驟
    1. 導入字體
      • 網上下載
      • 從C:/Windows/fonts裏面獲取導入
    2. 以黑體SimHei.ttf爲例
1. mkdir /usr/share/fonts/chinese           # 創建一個文件夾
2. cp SimHei.ttf /usr/share/fonts/chinese/
3. mkfontscale                              # 添加目錄和字體到配置文件,如果command not found 用yum -y install mkfontscale
4. mkfontdir
5. fc-cache                                 # 刷新字體緩存
6. fc-list :lang=zh                         # 查看中文字體

二、matplotlib亂碼問題

1. find |grep matplot|grep fonts             # 在根目錄下 查找matplotlib內置的fonts目錄
2. cp  SimHei.ttf /usr/local/lib64/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/ #將字體也複製給matplotlib一份
3. vim /usr/local/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
4. 去掉font.family、font.sans-serif的註釋,並在sans最後加入你的字體的名稱,eg:SimHei,同時令axes.unicode_minus = False ,確保中文減號正常顯示
5. rm -rf ~/.cache/matplotlib/*              # 清除matplotlib的字體緩存
6. 若是使用jupyter,重啓當前的運行的項目,重新執行代碼就可以看到方塊已經改爲中文了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章