大數據

解決中文問題 | Python 數據可視化庫 Matplotlib 快速入門之九

修改折線圖座標 | Python 數據可視化庫 Matplotlib 快速入門之八

解決中文問題

我們來說一下上一節遺留的中文顯示的問題。
首先來說明一下根本原因:在編寫Matplotlib所支持的字體的時候,字體本身不支持中文的顯示,所以遇到中文顯示出不來的情況。所以我們只需要改變成支持中文的字體就可以解決了。

下載中文字體(黑體,看準系統版本)

  • 下載SimHei字體(或者其他的支持中文顯示的字體也行)

mac的一次配置,一勞永逸
ubantu每創建一次新的虛擬環境,需要重新配置

1)安裝字體

  • windows和mac下:雙擊安裝
  • linux下:拷貝字體到usr/share/fonts下:
sudo cp ~/SimHei.ttf /usr/share/fonts/SimHei.ttf

注) Linux如果用ubantu也可以通過雙擊安裝

2) 刪除matplotlib緩存文件

image.png

Mac系統的解決方案:

  • 刪除~/.matplotlib中的緩存文件
cd ~/.matplotlib
rm -r *

Linux系統的解決方案

  • 刪除~/.cache/matplotlib中的緩存文件
cd ~/.cache/matplotlib
rm -r *

3)修改配置文件matplotlibrc

Mac系統的解決方案:

  • 修改配置文件matplotlibrc
vi~/.matplotlib/matplotlib rc

將文件內容修改為:

banckend:TkAgg
font.family          :  sans-serif
font.sans-serif          :  SimHei
axes.unicode_minus   :  False

Linux系統的解決方案

  • 修改配置文件
sudo find -name matplotlibrc

返回結果:

./.virtualenvs/ai/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc

打開配置文件:

vi ./.virtualenvs/ai/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc

將配置文件中下面3項改為如下所示:

font.family          :  sans-serif
font.sans-serif          :  SimHei
axes.unicode_minus   :  False

可以根據自己的系統去選擇不同的解決辦法,以上方法解決不了的,也可以自行找方法。

配套視頻課程,點擊這裡查看

獲取更多資源請訂閱Python學習站

Leave a Reply

Your email address will not be published. Required fields are marked *