Ask Your Question

draw a tree diagram

asked 2018-06-05 11:21:00 -0500

Sara gravatar image

updated 2018-06-05 11:21:21 -0500

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 s0s1s2s4s5s6s0s0s7 (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'})[4,4], graph_border=True) But the labels on the vertices are not readable. Really, I need something like a diagram not a graph.

edit retag flag offensive close merge delete


To 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)


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.

slelievre gravatar imageslelievre ( 2018-06-05 13:30:13 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2018-06-05 12:05:36 -0500

Sébastien gravatar image

updated 2018-06-05 12:09:38 -0500

You can do this using RecursivelyEnumeratedSet. 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-06-05 11:21:00 -0500

Seen: 32 times

Last updated: Jun 05