前提:python代碼執行報錯:
findfont: Generic family ‘sans-serif’ not found because none of the
following families were found: SimHei findfont: Generic family
‘sans-serif’ not found because none of the following families were
found: SimHei findfont: Generic family ‘sans-serif’ not found because
none of the following families were found: SimHei
/opt/module/python_shell/myoa/server.py:31: UserWarning: Glyph 20010
(\N{CJK UNIFIED IDEOGRAPH-4E2A}) missing from current font.
plt.savefig(save_path) /opt/module/python_shell/myoa/server.py:31:
UserWarning: Glyph 20154 (\N{CJK UNIFIED IDEOGRAPH-4EBA}) missing from
current font. plt.savefig(save_path)
/opt/module/python_shell/myoa/server.py:31: UserWarning: Glyph 20107
(\N{CJK UNIFIED IDEOGRAPH-4E8B}) missing from current font.
plt.savefig(save_path) /opt/module/python_shell/myoa/server.py:31:
UserWarning: Glyph 39033 (\N{CJK UNIFIED IDEOGRAPH-9879}) missing from
current font. plt.savefig(save_path) findfont: Generic family
‘sans-serif’ not found because none of the following families were
found: SimHei /opt/module/python_shell/myoa/server.py:31: UserWarning:
Glyph 20113 (\N{CJK UNIFIED IDEOGRAPH-4E91}) missing from current
font. plt.savefig(save_path)
/opt/module/python_shell/myoa/server.py:31: UserWarning: Glyph 26700
(\N{CJK UNIFIED IDEOGRAPH-684C}) missing from current font.
plt.savefig(save_path) /opt/module/python_shell/myoa/server.py:31:
UserWarning: Glyph 38754 (\N{CJK UNIFIED IDEOGRAPH-9762}) missing from
current font. plt.savefig(save_path)
疑問:本地的python是3.12版本,安裝了pip install matplotlib 之後,無沒有出現bug,但是把代碼打包到服務器上,報了錯誤,發現服務為的python版本是3.10版本
分析:
報這個錯誤,是因為少了 SimHei 字體
解決方案:
1.確定是否存在 SimHei 字體
import matplotlib
print(matplotlib.get_cachedir())
結果:
確定是少了SimHei字體
(2)官網下載SimHei:https://www.fontpalace.com/font-download/simhei/
建議去官網下載,比較安全
(3)將下載好的文件放到ttf路勁下,具體路徑是:/opt/module/miniconda3/lib/python3.10/site-packages/matplotlib/mpl-data/fonts/ttf
其中/opt/module/miniconda3/lib/python3.10是python安裝目錄
(4) 删除matplotlib的緩存字體
1.確定緩存路徑
import matplotlib
print(matplotlib.get_cachedir())
2.刪除輸出路徑的數據
5.修改matplotlibrc文件:路徑是:/opt/module/miniconda3/lib/python3.10/site-packages/matplotlib/mpl-data/matplotlibrc
修改內容如下:
# 去掉前面的#号
font.family: sans-serif
# 去掉前面的#号,并在:号后面加上SimHei
font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# 去掉前面的#号,并将True改为False
axes.unicode_minus: False