在 Python 中绘制树 [英] Tree plotting in Python

查看:27
本文介绍了在 Python 中绘制树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python 绘制树.决策树、组织结构图等.有什么图书馆可以帮助我吗?

I want to plot trees using Python. Decision trees, Organizational charts, etc. Any library that helps me with that?

推荐答案

有graphviz - http://www.graphviz.org/.它使用DOT"绘制图形的语言.您可以自己生成 DOT 代码,也可以使用 pydot - https://github.com/pydot/pydot.您也可以使用 networkx - http://networkx.lanl.gov/tutorial/tutorial.html#drawing-graphs,它可以轻松地绘制到 graphviz 或 matplotlib.

There's graphviz - http://www.graphviz.org/. It uses the "DOT" language to plot graphs. You can either generate the DOT code yourself, or use pydot - https://github.com/pydot/pydot. You could also use networkx - http://networkx.lanl.gov/tutorial/tutorial.html#drawing-graphs, which make it easy to draw to either graphviz or matplotlib.

networkx + matplotlib + graphviz 为您提供最大的灵活性和功能,但您需要安装很多.

networkx + matplotlib + graphviz gives you the most flexibility and power, but you need to install a lot.

如果您想要快速解决方案,请尝试:

If you want a quick solution, try:

安装 Graphviz.

Install Graphviz.

open('hello.dot','w').write("digraph G {Hello->World}")
import subprocess
subprocess.call(["path/to/dot.exe","-Tpng","hello.dot","-o","graph1.png"]) 
# I think this is right - try it form the command line to debug

然后你安装pydot,因为pydot已经为你做了这件事.然后你可以使用networkx来驱动"pydot.

Then you install pydot, because pydot already does this for you. Then you can use networkx to "drive" pydot.

这篇关于在 Python 中绘制树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆