python 由递归的dict构建树的画图代码

createPlot(mytree)方法实现。

其中myTree是一个字典,调用retrieveTree(0)可以获得一个字典的样式。

Last login: Thu Feb 23 19:07:53 on ttys000
B000000060143:~ zhanghao44$ ls
4        Desktop        IdeaProjects    Music        PycharmProjects
4a        Documents    Library        Pictures
CLionProjects    Downloads    Movies        Public
B000000060143:~ zhanghao44$ cd Desktop/
B000000060143:Desktop zhanghao44$ ls
help
machinelearninginaction
ps-dsp
终端
备份 
屏幕快照 2017-02-22 下午7.36.56.png
屏幕快照 2017-02-22 下午7.41.27.png
屏幕快照 2017-02-22 下午7.45.55.png
张浩出勤统计.xlsx
B000000060143:Desktop zhanghao44$ cd machinelearninginaction/
B000000060143:machinelearninginaction zhanghao44$ ls
Ch02        Ch05        Ch08        Ch11        Ch14
Ch03        Ch06        Ch09        Ch12        Ch15
Ch04        Ch07        Ch10        Ch13        README.rst
B000000060143:machinelearninginaction zhanghao44$ cd Ch03
B000000060143:Ch03 zhanghao44$ ls
classifierStorage.txt    treePlotter.py        trees.py
lenses.txt        treePlotter.pyc        trees.pyc
B000000060143:Ch03 zhanghao44$ vi trees.py
B000000060143:Ch03 zhanghao44$ vi treePlotter.py



































B000000060143:Ch03 zhanghao44$ vi treePlotter.py

    plotTree.totalD = float(getTreeDepth(inTree))
    plotTree.xOff = -0.5/plotTree.totalW; plotTree.yOff = 1.0;
    plotTree(inTree, (0.5,1.0), '')
    plt.show()

def createPlot1():
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
    plotNode('a decision node', (0.5, 0.1), (0.1, 0.5), decisionNode)
    plotNode('a leaf node', (0.8, 0.1), (0.3, 0.8), leafNode)
    plt.show()

def retrieveTree(i):
    listOfTrees =[{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}},
                  {'no surfacing': {0: 'no', 1: {'flippers': {0: {'head': {0: 'no', 1: 'yes'}}, 1: 'no'}}}}
                  ]
    return listOfTrees[i]

#createPlot(thisTree)
View Code