当在 Python 数据分析中遇到类似以下警告时:
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 24037 missing from current font.
font.set_text(s, 0.0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 36164 missing from current font.
font.set_text(s, 0.0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 27700 missing from current font.
font.set_text(s, 0.0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 24179 missing from current font.
font.set_text(s, 0.0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 24037 missing from current font.
font.set_text(s, 0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 36164 missing from current font.
font.set_text(s, 0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 27700 missing from current font.
font.set_text(s, 0, flags=flags)
D:\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 24179 missing from current font.
font.set_text(s, 0, flags=flags)
这通常是因为 Matplotlib 找不到合适的字体文件来渲染所需的字符,特别是对于非英文字符或特殊字符。这个问题可以通过以下方法解决:
解决方法:
1. 安装额外的字体文件
Matplotlib 默认使用系统中可用的字体,因此可以通过安装包含所需字符的字体来解决此问题。你可以搜索并安装包含中文字符的字体,如微软雅黑、宋体等。
2. 指定字体文件路径
你也可以手动指定字体文件的路径,告诉 Matplotlib 使用特定的字体。例如:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体
plt.rcParams['axes.unicode_minus'] = False # 解决负号无法显示的问题