Data-visualization-04graphvizdendrogram数字有一个重要的故事要讲。他们依靠你给他们一个清晰而有说服力的声音。--StephenFewData系列:目录|配置|代码仓库|对应实战在学习过程中,我们有时需要画树状图(比如决策树的表示,流程图等),所以在这一节我们使用graphviz库实现绘图。1.Graphviz介绍和配置Graphviz库通过创建图对象、添加节点和边来组装图,可以在JupyterNotebook和JupyterQt中渲染和显示图。使用anaconda安装,在终端输入命令condainstall-ndata_env_37python-graphviz(注意,如果使用conda安装graphviz而不是python-graphviz,会提示没有graphviz库)2.示例代码【基本绘图】'''是最基本的图'''fromgraphvizimportDigraphdot=Digraph('Graph1')dot.node('A','red',color='red')dot.node('B','blue',color='blue')dot.node('C','green',color='green')dot.edges(['AB','AC'])dot.edge('B','C',constraint='false',label='test')dot.attr(label=r'\ntestimage1\nred,greenandblue')dot.attr(fontsize='10')#exportimagedot.render(filename='04-01',directory='./',format='png',cleanup='true')dot【应用HTML】dot=Digraph('图2')dot.node('tab',label='''<
>''')[变换形状、线条和颜色]dot=Digraph(comment='Figure2',filename='Figure2.gv')dot.attr(rankdir='LR',size='8,5')dot.attr('node',shape='斗blecircle',style='solid')dot.node('S','Start',color='gray')dot.attr('node',shape='Msquare',style='bold')dot.node('E','End',color='gray')dot.attr('node',shape='circle',style='dashed',color='lightgrey')dot.node('A','节点A')dot.attr('node',shape='rarow',style='dotted',color='lightgrey')dot.node('B','节点B',color='cyan')dot.attr('node',shape='box',style='rounded')dot.node('C','节点C',color='lightblue2')dot.attr('node',shape='diamond',style='filled')dot.node('D','节点D',color='lightblue2:yellow',fontcolor='white')dot.attr('node',shape='egg',style='filled')dot.node('F','节点F',color='red:yellow',fontcolor='white')dot.attr('node',shape='point')dot.node('G','节点G')dot.edge('S','A',label='SA',color='gray')dot.edge('S','B',label='SB',color='gray',arrowsize='2.0')dot.attr('edge',style='dotted',penwidth='2.0')dot.edge('A','C',label='AC',color='lightgrey',arrowhead='vee')dot.edge('C','D',label='CD',color='lightgrey',arrowhead='normal')dot.attr('edge',style='dashed',penwidth='3.0')dot.edge('B','F',label='BF',color='lightblue2',arrowsize='2.0')dot.attr('edge',style='dashed',penwidth='2.0')dot.edge('D','E',label='DE',color='lightblue2',arrowhead='vee')dot.edge('F','E',label='FE',color='lightblue2',arrowhead='vee')dot.attr(label=r'\n测试图2')dot.attr(fontsize='20')【子图】g=有向图('G')g.attr(rankdir='TB',compound='true')withg.subgraph(name='cluster0')asc:c.edges(['ab','ac','bd','cd'])与g.subgraph(name='cluster1')作为c:c.edges(['eg','ef'])g.edge('b','f',lhead='cluster1')g.edge('d','e')g.edge('c','g',ltail='cluster0',lhead='cluster1')g.edge('c','e',ltail='cluster0')g.edge('d','h')参考资料Graphviz官方教程