# 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 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'}) 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.

edit retag 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)


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)


Sort by » oldest newest most voted

You can do this using RecursivelyEnumeratedSet. Here is one easy example :

sage: def f(a):
....:     return [2*a, 2*a+1]
sage: R = RecursivelyEnumeratedSet(, 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()


more
{"ft_lang_label":"__label__en","ft_lang_prob":0.80554885,"math_prob":0.8592757,"size":933,"snap":"2020-10-2020-16","text_gpt3_token_len":245,"char_repetition_ratio":0.095801935,"word_repetition_ratio":0.0,"special_character_ratio":0.25294748,"punctuation_ratio":0.16666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9934428,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-04T17:59:02Z\",\"WARC-Record-ID\":\"<urn:uuid:6264108c-d5f5-432f-aada-642890b7d235>\",\"Content-Length\":\"55831\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:28d57f3d-27a8-4432-a5de-6adbdb4fdb2c>\",\"WARC-Concurrent-To\":\"<urn:uuid:01fc9e83-82ca-4ea1-ba92-6299eacc695c>\",\"WARC-IP-Address\":\"140.254.118.68\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/42510/draw-a-tree-diagram/?sort=latest\",\"WARC-Payload-Digest\":\"sha1:QHETHPUIYTHRZJUUQFDTEX2YVBLLVDRK\",\"WARC-Block-Digest\":\"sha1:VHM5TP5GMUHCQCDABSQ2ADOZMMHWEYQB\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370524604.46_warc_CC-MAIN-20200404165658-20200404195658-00063.warc.gz\"}"}