对plotTree的解释

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)二者之间存在父节点指向子节点的箭头。

第八步完成

原文地址:https://www.cnblogs.com/Pigsss/p/12295693.html