Linux(deepin) 系統: 解決 matplotlib 中文亂碼問題


一、問題:

matplotlib 庫設置中文顯示時會出現小方框,為了解決這個問題查閱了很多資料,和Ubuntu,centos等Linux系統解決方法不一樣,按照查詢方法無法解決問題,因此查了很久之后找到了解決方法。

 

 二、解決方法

1. 下載字體
  下載中文字體 SimHei.ttf

2. 刪除當前用戶 matplotlib 的緩沖文件

# 終端執行下面命令
cd ~/.cache/matplotlib rm -rf *.*

3. 添加字體
首先在終端中進入你的環境,查看 matplotlib 配置文件位置

# 切換到你的虛擬環境
workon '虛擬環境名'

# 進入python交互模式:
ipython3

# 導入matplotlib包
import matplotlib

# 查看matplotlib配置文件所在位置
print(matplotlib.matplotlib_fname())

# 第二種較為方便查看方式
import matplotlib as mpl
print(mpl.get_configdir())

4. 進入配置文件所在對應文件夾

將下載的字體放到 fonts/ttf 文件夾里面,(*.ttf放到這個文件夾里面)
5. 編輯配置文件 matplotlibrc

# 利用搜索關鍵字找到下面這一行代碼,然后去掉 # (取消注釋),添加下載的字體 SimHei (字體名,我的是SimHei)

# 源內容
#font.sans-serif:DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

修改為

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

重啟 jupyter-notebook

運行成功

 

 收工

 

三、補充安裝問題:

  直接pip下載會比較慢,下載的時候指定源會快許多。如下:

pip install matplotlib -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install numpy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pandas -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install seaborn scipy  -i http://pypi.douban.com/simple --trusted-host pypi.douban.com


免責聲明!

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



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