Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree plotting in Python

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

like image 744
Injeniero Barsa Avatar asked Oct 06 '11 04:10

Injeniero Barsa


People also ask

What is value in decision tree plot?

The value line in each box is telling you how many samples at that node fall into each category, in order. That's why, in each box, the numbers in value add up to the number shown in sample . For instance, in your red box, 91+212+113=416.


Video Answer


2 Answers

I develop ETE, which is a python package intended, among other stuff, for programmatic tree rendering and visualization. You can create your own layout functions and produce custom tree images: enter image description here

It has a focus on phylogenetics, but it can actually deal with any type of hierarchical tree (clustering, decision trees, etc.)

like image 136
jhc Avatar answered Sep 21 '22 19:09

jhc


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 gives you the most flexibility and power, but you need to install a lot.

If you want a quick solution, try:

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 

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

like image 33
wisty Avatar answered Sep 20 '22 19:09

wisty