在Matplotlib中的注解绘制树形图中文不能正确显示-机器学习实战第三篇决策树

问题:机器学习实战第三章决策树中,遇到树形图中文不显示

源代码如下:

# decision_graphic_tree.py
import matplotlib.pyplot as plt

decisionNode = dict(boxstyle="sawtooth", fc="0.8")
leftNode = dict(boxstyle="round4", fc="0.8")
arrow_args = dict(arrowstyle="<-")

def plotNode(nodeTxt, centerPt, parentPt, nodeType):
    createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction',\
                            xytext=centerPt, textcoords='axes fraction',\
                            va="center", ha="center", bbox=nodeType, arrowprops=arrow_args)

def createPlot():
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    createPlot.ax1 = plt.subplot(111, frameon=False)
    plotNode('决策节点', (0.5, 0.1), (0.1, 0.5), decisionNode)
    plotNode('叶节点', (0.8, 0.1), (0.3, 0.8), leftNode)
    plt.show()

在pycharm console中输入:

>>>import decision_graphic_tree
>>>decision_graphic_tree.createPlot()

出现以下的错误

F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:214: RuntimeWarning: Glyph 20915 missing from current font.
  font.set_text(s, 0.0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:214: RuntimeWarning: Glyph 31574 missing from current font.
  font.set_text(s, 0.0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:214: RuntimeWarning: Glyph 33410 missing from current font.
  font.set_text(s, 0.0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:214: RuntimeWarning: Glyph 28857 missing from current font.
  font.set_text(s, 0.0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:214: RuntimeWarning: Glyph 21494 missing from current font.
  font.set_text(s, 0.0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:183: RuntimeWarning: Glyph 20915 missing from current font.
  font.set_text(s, 0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:183: RuntimeWarning: Glyph 31574 missing from current font.
  font.set_text(s, 0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:183: RuntimeWarning: Glyph 33410 missing from current font.
  font.set_text(s, 0, flags=flags)
F:\Program\python program\MachineLearningInAction\venv\lib\site-packages\matplotlib\backends\backend_agg.py:183: RuntimeWarning: Glyph 28857 missing from current font.
  font.set_text(s, 0, flags=flags)

显示图片中的中文显示只有格子:
中文显示为格子了
这种一般是字体设置之类的错误,于是在网上查找一些解决办法:

解决办法:设置字体环境

plt.rcParams['font.sans-serif'] = ['simHei'] #指定默认字体
>>>from importlib import reload
>>>reload(decision_graphic_tree)
<module 'decision_graphic_tree' from 'F:\\Program\\python program\\MachineLearningInAction\\part_3\\decision_graphic_tree.py'>
>>>decision_graphic_tree.createPlot()

图片中正常显示中文字符:
正常显示

参考

https://blog.csdn.net/jiangsujiangjiang/article/details/84333144

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼儿LY

一切随缘

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值