1.>>>a = 1/2/2
   >>>a

>>>0.25

2.def plotMidText(cntrPt,parentPt,txtString):                                                    #在父子节点间填充文本信息
    xMid = (parentPt[0]-cntrPt[0])/2.0 + cntrPt[0]
    yMid = (parentPt[1]-cntrPt[1])/2.0 + cntrPt[1]
    createPlot.ax1.text(xMid,yMid,txtString)                                  #x和y的坐标轴  和填充内容
   
#为了方便理解这个函数我设定getNumLeafs=3   getDepth=2  
1.def plotTree(myTree,parentPt,nodeTxt):
2.      numLeafs = getNumLeafs(myTree)
3.      depth = getTreeDepth(myTree)                           #这个变量没有用到
4.      firstStr = list(myTree.keys())[0]                       #得到字典的第一个键
5.      cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW,                      
6.             plotTree.yOff)                                                                         #计算子节点的坐标(0.5,1.0)下面会改变哟
7.      plotMidText(cntrPt,parentPt,nodeTxt)
8.      plotNode(firstStr,cntrPt,parentPt,decisionNode)                                     
9.      secondDict = myTree[firstStr]                                                  #{0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}, 3: 'maybe'}
10.    plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD                        #到这里子系欸但坐标 (0.5,0.5)        
11.    for key in secondDict.keys():
12.         if type(secondDict[key]).__name__ == 'dict':
13.         plotTree(secondDict[key],cntrPt,str(key))                                #递归调用plotTree
14.    else:
15.         plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW
16.         plotNode(secondDict[key],(plotTree.xOff,plotTree.yOff),
17.                        cntrPt,leafNode)                                                    #调用上面的函数plotNode()
18.         plotMidText((plotTree.xOff,plotTree.yOff),cntrPt,str(key))
19. plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD
   
#为了方便理解这个函数我设定numLeafs=3   depth=2       
def createPlot(inTree):                                                    #画图函数
    fig = plt.figure(1,facecolor = 'white')
    fig.clf()                                                                            #清空画布
    axprops = dict(xticks=[],yticks=[])                       #此参数表示坐标刻度,[]表示不显示刻度,可以作为参数传入,也可以用plt.xticks([1,3,4])单独设置
    createPlot.ax1 = plt.subplot(111,frameon=False,**axprops)   
    plotTree.totalW = float(getNumLeafs(inTree))                         #宽度=3.0
    plotTree.totalD = float(getTreeDepth(inTree))                         #深度=2.0
    plotTree.xOff = -0.5/plotTree.totalW;plotTree.yOff = 1.0;        #得到两个全局变量 x和y  (-1/6,1.0)
    plotTree(inTree,(0.5,1.0),'')                                                     #(0.5,1.0)顶层节点的坐标
    plt.show()       
   这段代码个人觉得比较难理解(为了方便理解我们这里将宽度设置为3.0,深度设置为2.0)顶点坐标为(0.5,1.0)采取的myTree=

{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}

第一步 我们画图时候调用的是createPlot()这个函数,从函数我们得知   两个全局变量(-1/6,1.0)即xOff,yOff

第二步 我们看到该函数中存在plotTree(inTree,(0.5,1.0),'') 这段代码,即调用了上面一个函数plotTree,经过计算我们得到子节点的第一个坐标(0.5,1.0)  即函数中第6行

第三步通过调用plotMidText和plotNode将顶点坐标内容(这里填充的内容是键的名称)填充和设置格式,即顶点坐标(这里因为子节点和父节点是同一个点,所以顶点没有指向的自己的箭头)

第四步我们得到myTree第一个键对应的值并设为字典secondDict,第10行计算变量y的新值0.5,注意子节点此时还是(0.5,1.0)

第五步遍历secondDict中的所有键,如果键对应的值是字典呢么递归调用plotTree注意里面的参数子节点(0.5,1.0)是panterPt的新参数,myTree对应的参数是secondDict[key]

第六步由给出myTree得知for循环中存在不是字典的值,呢么进入else:改变了xOff的值(经计算的为1/6)调用plotNode(secondDict[key],(1/6,0.5),(0.5,1.0),leafnode)即该处为叶子

第六步我们接着第五步递归调用,此时numLeafs变为2,depth变为1(这个depth没用到可以忽略)我们还是取新字典的第一个键,计算子节点得到(2/3,0.5)

第七步同第三步骤,不过这里子节点和父节点不一致子节点(2/3,0.5)父节点(0.5,1.0)二者之间存在父节点指向子节点的箭头。

第八步完成

最新文章

  1. Point Grey FlyCapture 实例汇总
  2. 解决 Android SDK下载和更新失败“Connection to https://dl-ssl.google.com refused."
  3. 数值积分NIntegrate中的具体算法
  4. xxx-servlet.xml vs applicationContext.xml
  5. 总结了关于PHP xss 和 SQL 注入的问题(转)
  6. MySQL集群(四)之keepalived实现mysql双主高可用
  7. Android性能提升之强引用、软引用、弱引用、虚引用使用
  8. Log4j2中RollingFile的文件滚动更新机制
  9. matplotlib柱状图-【老鱼学matplotlib】
  10. python各种类型日期转换大全
  11. osx安装sass
  12. jenkins 邮箱设置
  13. Linux使用退格键时出现^H ^?解决方法
  14. JAVA基础知识笔记
  15. 【转载】unittest参数化(paramunittest)
  16. JPI中常使用的类介绍:
  17. Digitalocean + ss 搭建加密通信代理服务器
  18. 【C#】【MySQL】C# 查询数据库语句@Row:=@Row+1以及执行存储过程失败解决方案
  19. 虚拟机中安装 centOS,本地安装 SSH 连接 - 02
  20. zt 李鸿章听过《彩云追月》?

热门文章

  1. Python 基础之python运算符
  2. 输出复选框选中的文件名 checkbox
  3. 题解 LG P2264
  4. WebApi如何接收前台传递过来的数组
  5. Android 4.1 设置默认开机动态壁纸
  6. java递归调用 return的问题
  7. PLSQL Developer配置Oralce11g连接
  8. mysql dump 完全备
  9. 二 配置数据字典&异步查询客户
  10. JavaScript 词法句法