ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 29 Mar 2011 14:07:08 +0200Treeplot()?https://ask.sagemath.org/question/8032/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!
Tue, 29 Mar 2011 12:49:35 +0200https://ask.sagemath.org/question/8032/treeplot/Answer by cswiercz for <p>Hi, </p>
<p>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:</p>
<pre><code>01110000
10001100
10000110
10001010
01010000
01100000
00110001
00001110
</code></pre>
<p>Can anyone help me? (Maybe I have to use networkx somehow...).</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8032/treeplot/?answer=12235#post-id-12235To 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])
sage: G = Graph(M,format='adjacency_matrix')
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.Tue, 29 Mar 2011 14:07:08 +0200https://ask.sagemath.org/question/8032/treeplot/?answer=12235#post-id-12235