一、解決Linux亂碼問題
- 問題出現在運行matplotlib圖片的title亂碼
2.步驟
- 導入字體
- 網上下載
- 從C:/Windows/fonts裏面獲取導入
- 以黑體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,重啓當前的運行的項目,重新執行代碼就可以看到方塊已經改爲中文了