# Treeplot()?

Hi,

I'm aware that Mathematica has a treeplot() function which basically plots a rooted tree. I've been searching for a while now to see if Sage has similar functionality. I basically want to plot a tree diagram with the following adjacency matrix:

01110000
10001100
10000110
10001010
01010000
01100000
00110001
00001110


Can anyone help me? (Maybe I have to use networkx somehow...).

Thanks!

edit retag close merge delete

Sort by ยป oldest newest most voted

To create a graph from an adjacency matrix, use the following:

sage: M = matrix(ZZ,8,8,[0,1,1,1,0,0,0,0,
1,0,0,0,1,1,0,0,
1,0,0,0,0,1,1,0,
1,0,0,0,1,0,1,0,
0,1,0,1,0,0,0,0,
0,1,1,0,0,0,0,0,
0,0,1,1,0,0,0,1,
0,0,0,0,1,1,1,0])


You can find more ways to create graphs by running "Graph?" in Sage. To plot this graph you use the Graph.plot() command.

sage: G.plot()


If the graph is a tree there's a special argument you can provide to plot() that will make the formatting nicer.

sage: G.plot(layout='tree')
Traceback (click to the left of this block for traceback)
...
RuntimeError: Cannot use tree layout on this graph: self.is_tree()
returns False.


However, the graph you provided isn't really a tree. Run the first three commands above to see what I mean, just in case I entered your adjacency matrix incorrectly. Finally, if you do have a tree running

sage: G.plot?


will explain various other plotting options that might prove useful. In particular, options for specifying tree roots and such.

more