mac OS matplotlib missing from font(s) DejaVu Sans

如果能搜索到这篇文章,我猜你遇到了和我一样的问题:matplotlib绘图中文乱码。如下:
在这里插入图片描述
出现这个问题的原因是:matplotlib使用的字体列表中默认没有中文字体。
这里说一种解决方案:我们可以在文件中手动指定matplotlib使用的字体
在python文件中指定matplotlib使用的字体,如下:

plt.rcParams['font.sans-serif']=['STFangsong'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号

完整代码:

from pylab import mpl
from sklearn.tree import DecisionTreeClassifier, plot_tree
import numpy as np
import matplotlib.pyplot as plt

#手动指定matplotlib使用的字体
plt.rcParams['font.sans-serif']=['STFangsong'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号

# 创建数据集
X = np.array([
    [0, 2, 0],  # 晴天,高温,无风
    [1, 1, 1],  # 阴天,中温,微风
    [2, 0, 2],  # 雨天,低温,强风
    # ... 添加更多样本以增加模型的准确性
])
y = np.array([0, 1, 2])  # 分别对应去野餐、去博物馆、在家看书

# 初始化决策树模型,设置最大深度为5
clf = DecisionTreeClassifier(max_depth=5, random_state=42)

# 训练模型
clf.fit(X, y)

# 可视化决策树
plt.figure(figsize=(20, 10))
plot_tree(clf, filled=True, feature_names=["天气状况", "温度", "风速"], class_names=["去野餐", "去博物馆", "在家看书"], rounded=True, fontsize=12)
plt.show()

然后清除一下matplotlib的缓存

rm -rf ~/.matplotlib

之后再重新执行python文件,就不会乱码了
在这里插入图片描述

D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 32428 (\N{CJK UNIFIED IDEOGRAPH-7EAC}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 35009 (\N{CJK UNIFIED IDEOGRAPH-88C1}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 21098 (\N{CJK UNIFIED IDEOGRAPH-526A}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 21518 (\N{CJK UNIFIED IDEOGRAPH-540E}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 25454 (\N{CJK UNIFIED IDEOGRAPH-636E}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 32463 (\N{CJK UNIFIED IDEOGRAPH-7ECF}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 39640 (\N{CJK UNIFIED IDEOGRAPH-9AD8}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 31243 (\N{CJK UNIFIED IDEOGRAPH-7A0B}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 65288 (\N{FULLWIDTH LEFT PARENTHESIS}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 31859 (\N{CJK UNIFIED IDEOGRAPH-7C73}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self) D:\py\PyCharm2024.1.7\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:80: UserWarning: Glyph 65289 (\N{FULLWIDTH RIGHT PARENTHESIS}) missing from font(s) DejaVu Sans. FigureCanvasAgg.draw(self)
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值