jupyter-notebook解決中文亂碼

進入jupyter-notebook的安裝環(huán)境

cd /opt/Jupyter-Notebook/notebook-6.4.0

打開python虛擬環(huán)境(這里有autoenv自動打開,但是本質(zhì)上依然要打開python的env環(huán)境)

source /opt/Jupyter-Notebook/env/bin/activate

檢查有沒有指定的包

pip list|grep matplotlib

如果有的話繼續(xù)下一步

打開ipython

root@jupyter-notebook-1 notebook-6.4.0 # ipython
Python 3.7.9 (default, Jun 20 2021, 18:28:19) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.24.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import matplotlib
(1號位解釋說明)
Duplicate key in file PosixPath('/opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'), line 258 ('font.family:  sans-serif')
Duplicate key in file PosixPath('/opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'), line 266 ('font.serif:      DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif')
Duplicate key in file PosixPath('/opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'), line 411 ('axes.unicode_minus: False  # use Unicode for the minus symbol rather than hyphen.  See')

In [2]: print(matplotlib.matplotlib_fname())
(2號位解釋說明)
/opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

In [3]:

1號位說明:

這是一個python的常規(guī)報錯,因為我在下文中修改指定文件的時候,選擇的是復(fù)制一行而不是直接在原文上修改。因此這里報錯:Duplicate key in file PosixPath
報錯意思是:文件PosixPath中存在重復(fù)密鑰

2號位說明:

這個位置就是matplotlib模塊的安裝位置,要記得env環(huán)境里的包安裝位置與全局環(huán)境中的包安裝位置是不同的,注意區(qū)分。

我們找到上文中的這個包安裝位置之后,就可以開始進行下一步的操作了:

cd /opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/
ls 

drwxr-xr-x 5 root root  4096 Jun 20 18:52 fonts
drwxr-xr-x 2 root root  4096 Jun 20 18:52 images
-rw-r--r-- 1 root root 41012 Jul  1 12:15 matplotlibrc
drwxr-xr-x 2 root root  4096 Jun 20 18:52 plot_directive
drwxr-xr-x 3 root root  4096 Jun 20 18:52 sample_data
drwxr-xr-x 2 root root  4096 Jun 20 18:52 stylelib

進入指定的目錄并下載中文字體文件:

cd /opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/

wget https://xxxxxxxxxxxxxxxxxxxx/bao/zabbix/simkai.ttf  (這個字體文件是以前處理zabbix上的圖表中文化時用的中文字體文件,可以聯(lián)系作者獲取該字體)

字體加載好之后,然后修改matplotlib模塊的全局配置文件

vim /opt/Jupyter-Notebook/env/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

第一處修改:
找到 font.family ,然后去掉注釋,不用做任何修改

第二處修改:
找到 font.sans-serif ,去掉注釋之后,然后再他的值中添加simkai字體的名稱。添加之后的內(nèi)容如下:
font.sans-serif: simkai, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
(其中:simkai就是我們放在指定目錄里的字體文件的名稱)

第三處修改:
找到:axes.unicode_minus,去掉注釋后,把他的值從True修改為False

最后保存這個文件

matplotlib模塊的全局配置文件修改好之后,去查看:/usr/lib/systemd/system/jupyter_notebook.service 文件,這個文件中規(guī)定了該服務(wù)器的啟動用戶是誰,從下文中可以看到該服務(wù)的啟動用戶是root用戶

[Unit]
Description=jupyter_notebook
After=network.target


[Service]
Type=simple
User=root
Group=root
LimitNOFILE=100000
LimitNPROC=100000
WorkingDirectory=/opt/demo/
ExecStart=/opt/Jupyter-Notebook/env/bin/python3 /opt/Jupyter-Notebook/env/bin/jupyter notebook --allow-root --ip=0.0.0.0 --port=8888
Restart=no


[Install]
WantedBy=multi-user.target

上文中,找到啟動用戶之后,去啟動用戶的根目錄下找到matplotlib模塊的緩存目錄,直接刪除這個緩存目錄

cd /root/.cache/

rm -rf ./matplotlib

重啟matplotlib的服務(wù)器

systemctl restart jupyter_notebook.service
systemctl status jupyter_notebook.service

此時再次運行一下上文中【打開iptyhon這個步驟,引用一下matplotlib模塊】然后就可以看到用戶的緩存目錄中,又出現(xiàn)了新的matplotlib的緩存文件。然后打開這個緩存文件
(經(jīng)過實際測試,重復(fù)上面的ipython這個步驟,好像不會生成緩存,只有必須在jupyter-notebook中調(diào)用這個函數(shù)才會生成緩存)

vim /root/.cache/matplotlib/fontlist-v330.json

搜索關(guān)鍵字:simkai,可以看到這個字體的所有被解析出來的屬性:
    {
      "fname": "fonts/ttf/simkai.ttf",
      "name": "KaiTi",
      "style": "normal",
      "variant": "normal",
      "weight": 400,
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },

此時注意一下name這個屬性,這就是后面python代碼執(zhí)行時,需要引用的字體名稱。

到此為止全局配置文件算是搞完了。

測試內(nèi)容如下:

打開jupyter-notebook,粘貼輸入以下代碼:

import matplotlib as plt
import seaborn as sns
import pandas as pd

tongji = pd.read_csv('#csv')
sns.set_context({"figure.figsize": (16, 9)})
sns.set_style({'font.sans-serif': 'KaiTi'})
plt.rcParams['font.sans-serif'] = ['KaiTi']
sns.barplot(data=tongji, x=douyin.category, y=douyin.fans)

最后就會得到一張包含中文字體的圖片,如下圖所示:


image.jpeg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容