ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 05 Jun 2018 13:30:13 -0500draw a tree diagramhttp://ask.sagemath.org/question/42510/draw-a-tree-diagram/Good afternoon,
I have defined a recursive function and I would like to vizualise the 'calls' of the function trought a tree diagram. The nodes should be the elements of a group (Extended affine weyl group). So expressions of this type s0*s1*s2*s4*s5*s6*s0*s0*s7 (not hashable). Is there a easy way to do that ? Maybe using just matplotlib? So without instal graphviz. I need something simple like latex \tree. I have just tried something like this
G = graphs.RandomGNM(4,5)
G.relabel({0:'zero' , 1:'one', 2:'two', 3:'three'})
G.show(figsize=[4,4], graph_border=True)
But the labels on the vertices are not readable. Really, I need something like a diagram not a graph.
Tue, 05 Jun 2018 11:21:00 -0500http://ask.sagemath.org/question/42510/draw-a-tree-diagram/Comment by slelievre for <p>Good afternoon,
I have defined a recursive function and I would like to vizualise the 'calls' of the function trought a tree diagram. The nodes should be the elements of a group (Extended affine weyl group). So expressions of this type s0<em>s1</em>s2<em>s4</em>s5<em>s6</em>s0<em>s0</em>s7 (not hashable). Is there a easy way to do that ? Maybe using just matplotlib? So without instal graphviz. I need something simple like latex \tree. I have just tried something like this
G = graphs.RandomGNM(4,5)
G.relabel({0:'zero' , 1:'one', 2:'two', 3:'three'})
G.show(figsize=[4,4], graph_border=True)
But the labels on the vertices are not readable. Really, I need something like a diagram not a graph. </p>
http://ask.sagemath.org/question/42510/draw-a-tree-diagram/?comment=42513#post-id-42513To display inline code, like `z = x*y`, use backticks.
To display blocks of code or error messages, skip a line above and below,
and do one of the following (all give the same result):
- indent all code lines with 4 spaces
- select all code lines and click the "code" button (the icon with '101 010')
- select all code lines and hit ctrl-K
For instance, typing
If we define `f` by
def f(x, y):
return (x, y)
then `f(2, 3)` returns `(2, 3)` but `f(2)` gives:
TypeError: f() takes exactly 2 arguments (1 given)
produces:
> If we define `f` by
>
> def f(x, y):
> return (x, y)
>
> then `f(2, 3)` returns `(2, 3)` but `f(2)` gives:
>
> TypeError: f() takes exactly 2 arguments (1 given)
Please edit your question to do that.Tue, 05 Jun 2018 13:30:13 -0500http://ask.sagemath.org/question/42510/draw-a-tree-diagram/?comment=42513#post-id-42513Answer by Sébastien for <p>Good afternoon,
I have defined a recursive function and I would like to vizualise the 'calls' of the function trought a tree diagram. The nodes should be the elements of a group (Extended affine weyl group). So expressions of this type s0<em>s1</em>s2<em>s4</em>s5<em>s6</em>s0<em>s0</em>s7 (not hashable). Is there a easy way to do that ? Maybe using just matplotlib? So without instal graphviz. I need something simple like latex \tree. I have just tried something like this
G = graphs.RandomGNM(4,5)
G.relabel({0:'zero' , 1:'one', 2:'two', 3:'three'})
G.show(figsize=[4,4], graph_border=True)
But the labels on the vertices are not readable. Really, I need something like a diagram not a graph. </p>
http://ask.sagemath.org/question/42510/draw-a-tree-diagram/?answer=42511#post-id-42511You can do this using [RecursivelyEnumeratedSet](http://doc.sagemath.org/html/en/reference/sets/sage/sets/recursively_enumerated_set.html#). Here is one easy example :
sage: def f(a):
....: return [2*a, 2*a+1]
sage: R = RecursivelyEnumeratedSet([1], f)
sage: R
A recursively enumerated set (breadth first search)
sage: G = R.to_digraph(max_depth=4)
sage: G
Looped multi-digraph on 63 vertices
sage: view(G)
Using optional package slabbe (installed with `sage -pip install slabbe`) + graphviz + dot2tex, you may draw a nicer looking graph:
sage: from slabbe import TikzPicture
sage: tikz = TikzPicture.from_graph(G)
sage: tikz.pdf()
See also the answer to [this other question](https://ask.sagemath.org/question/39425/tree-of-vectors/#39428).Tue, 05 Jun 2018 12:05:36 -0500http://ask.sagemath.org/question/42510/draw-a-tree-diagram/?answer=42511#post-id-42511