matplotlib 與 seaborn 中出現中文亂碼的解決方法(ubuntu下親測有用)

原文:https://www.cnblogs.com/ToDoToTry/p/7793137.html

Linux、Mac osx 系統中,出現 matplotlib 或 seaborn 繪圖中有中文亂碼的情形,可以考慮使用以下方式處理:
到 anaconda 的 matplotlib 中查看是否有 simhei.ttf 字體:

cd ~/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf
ls -al | grep simhei

如果沒有,從 windows 中用 everything 搜索全局文件,找到 simhei.ttf,並將其上傳到linux 的 matplotlib 的 fonts/ttf 文件夾
修改配置文件~/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc 文件,
將該文件拷貝到.cache/matplotlib 目錄下,並找到以下兩行,改爲如下:
  

font.family : sans-serif
font.sans-serif : simhei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

(linux)刪除~/.cache/matplotlib/目錄下的 fonts cache lis
PS: ubuntu下刪除fontList.json文件,運行一次ipython會自動生成

代碼設置 matplotlib 和 seaborn 的環境
  

import matplotlib as mpl
mpl.rcParams[‘font.sans-serif’] = [‘simhei’]
mpl.rcParams[‘font.serif’] = [‘simhei’]
import seaborn as sns
sns.set_style(“darkgrid”,{“font.sans-serif”:[‘simhei’,’Droid Sans Fallback’]})

參考:
https://github.com/mwaskom/seaborn/issues/1009
http://www.th7.cn/Program/Python/201704/1156781.shtml
作者: fandyst
出處: http://www.cnblogs.com/todototry/
關注語言: python、javascript(node.js)、objective-C、java、R、C++
興趣點: 互聯網、大數據技術、大數據IO瓶頸、col-oriented DB、Key-Value DB、數據挖掘、模式識別、deep learning、開發與成本管理
產品: 豬哥網,豬場管理軟件,畜牧行業

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