Ask Your Question
3

Best way to plot graph

asked 2011-07-14 17:23:01 +0100

Eugene gravatar image

Hello!

Could you pleas advise me the best way to plot graph with cycles and make it looks like tree? Here is the sample picture of result I'd like to have:

image description

Plot on this pictures have 'layers', is there any chance to get something like this in Sage? I tried this:

t = DiGraph()
t.add_edge((0,1))
t.add_edge((0,2))
t.add_edge((0,3))
t.add_edge((0,4))

t.add_edge((4,9))
t.add_edge((3,9))
t.add_edge((2,9))
t.add_edge((1,9))

t.plot()

but the result is far from desired.

Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
6

answered 2011-07-14 19:20:09 +0100

parzan gravatar image

updated 2011-07-15 06:35:48 +0100

You can use:

t.plot(pos=t.layout_acyclic())

If you get arrows pointing up it means you don't have the dot2tex package installed (you can check this with sage.graphs.dot2tex_utils.have_dot2tex()), and sage does the positioning itself (by calling t.layout_acyclic_dummy()). Just run install_package('dot2tex-2.8.7-2') (perhaps the version number will change one day, type optional_packages() to see all packages available).

You can also manually flip sage's positioning:

l = t.layout_acyclic_dummy()
t.plot(pos=dict([[v,[l[v][0],-l[v][1]]] for v in t.vertices()]))

or rotate it like in the image in the question:

l = t.layout_acyclic_dummy()
t.plot(pos=dict([[v,[l[v][1],l[v][0]]] for v in t.vertices()]))
edit flag offensive delete link more

Comments

This method works great, thank you! But is it possible somehow change orientation of the result tree-like graph? Something like tree_orientation='down' effect for actual tree.

Eugene gravatar imageEugene ( 2011-07-15 01:32:00 +0100 )edit

Ok, I checked what happens and updated my answer.

parzan gravatar imageparzan ( 2011-07-15 05:52:15 +0100 )edit

Thank you! This solution works perfectly correct.

Eugene gravatar imageEugene ( 2011-07-15 14:16:19 +0100 )edit

Glad to help.

parzan gravatar imageparzan ( 2011-07-15 18:57:46 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2011-07-14 17:23:01 +0100

Seen: 808 times

Last updated: Jul 15 '11