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.Mon, 10 Sep 2012 11:24:06 -0500Strategies for drawing good graphs (graph theory)http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/Okay, if I want to have a nice drawing of a 5-cycle, it's built in. If I do
g = graphs.CycleGraph(5)
g.show()
it automatically draws it in a nice way. But, if I am looking at some random graph of order 11 that I want a nice picture of, for my dissertation, how am I supposed to get a nice picture? According to my question [here](http://ask.sagemath.org/question/879/graph-editor-doesnt-workhttp://ask.sagemath.org/question/879/graph-editor-doesnt-work), kcrisman says the graph editor is broken. That would have been a nice tool. My current strategy is to repeatedly have it show the graph and save it as a pdf until it gets into the format I want, i.e., run the following code over and over.
graph_plot = g.plot(vertex_colors={'white'})
graph_plot.show()
graph_plot.save('graph.pdf')
But, this doesn't always end up with the picture I really want. And, occasionally I want to do something like delete a vertex and draw the graph again, but now all of the sudden the plot behaves completely differently and it never looks anything like the previous drawing minus one vertex.
So, what are some ways to get around this?Sat, 08 Sep 2012 04:11:03 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/Comment by dazedANDconfused for <p>Okay, if I want to have a nice drawing of a 5-cycle, it's built in. If I do</p>
<pre><code>g = graphs.CycleGraph(5)
g.show()
</code></pre>
<p>it automatically draws it in a nice way. But, if I am looking at some random graph of order 11 that I want a nice picture of, for my dissertation, how am I supposed to get a nice picture? According to my question <a href="http://ask.sagemath.org/question/879/graph-editor-doesnt-workhttp://ask.sagemath.org/question/879/graph-editor-doesnt-work">here</a>, kcrisman says the graph editor is broken. That would have been a nice tool. My current strategy is to repeatedly have it show the graph and save it as a pdf until it gets into the format I want, i.e., run the following code over and over.</p>
<pre><code>graph_plot = g.plot(vertex_colors={'white'})
graph_plot.show()
graph_plot.save('graph.pdf')
</code></pre>
<p>But, this doesn't always end up with the picture I really want. And, occasionally I want to do something like delete a vertex and draw the graph again, but now all of the sudden the plot behaves completely differently and it never looks anything like the previous drawing minus one vertex.</p>
<p>So, what are some ways to get around this?</p>
http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19087#post-id-19087There really isn't an agreed method to drawing a nice graph so I doubt a working graph editor would solve the problem. Here are [some heuristics that go into deciding if the graph looks nice](http://en.wikipedia.org/wiki/Graph_drawing) but these promptly get thrown out the window for special classes of graphs. Things will look best if you do it yourself using LaTeX. [Altermundus](http://altermundus.com/pages/links/index.html) has developed the packages using Tikz; his "Gallery of Named Graphs" has 3 versions of the Petersen graph starting on page 73. Which looks best? Most expect it to be drawn using form 1.Sun, 09 Sep 2012 08:53:50 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19087#post-id-19087Comment by G-Sage for <p>Okay, if I want to have a nice drawing of a 5-cycle, it's built in. If I do</p>
<pre><code>g = graphs.CycleGraph(5)
g.show()
</code></pre>
<p>it automatically draws it in a nice way. But, if I am looking at some random graph of order 11 that I want a nice picture of, for my dissertation, how am I supposed to get a nice picture? According to my question <a href="http://ask.sagemath.org/question/879/graph-editor-doesnt-workhttp://ask.sagemath.org/question/879/graph-editor-doesnt-work">here</a>, kcrisman says the graph editor is broken. That would have been a nice tool. My current strategy is to repeatedly have it show the graph and save it as a pdf until it gets into the format I want, i.e., run the following code over and over.</p>
<pre><code>graph_plot = g.plot(vertex_colors={'white'})
graph_plot.show()
graph_plot.save('graph.pdf')
</code></pre>
<p>But, this doesn't always end up with the picture I really want. And, occasionally I want to do something like delete a vertex and draw the graph again, but now all of the sudden the plot behaves completely differently and it never looks anything like the previous drawing minus one vertex.</p>
<p>So, what are some ways to get around this?</p>
http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19079#post-id-19079@dazedANDconfused Good point there, but I'm not worried about making THE best looking graph. I'm worried about A pretty good looking one. For example, in DSMs answer below, it's obvious that the second version is MUCH better than the first. I don't care if there is some other even better version out there, I would be fine with that second version. With the graph editor, I would be able to move the vertices around as I please and put it in a form that seems pretty nice. Without that, the answer by DSM seems to be very helpful. Use many iterations to get one that looks pretty nice. Save positions if I want to do anything like delete a vertex, and then the new graph obtained still has the same positions.Mon, 10 Sep 2012 04:22:01 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19079#post-id-19079Answer by DSM for <p>Okay, if I want to have a nice drawing of a 5-cycle, it's built in. If I do</p>
<pre><code>g = graphs.CycleGraph(5)
g.show()
</code></pre>
<p>it automatically draws it in a nice way. But, if I am looking at some random graph of order 11 that I want a nice picture of, for my dissertation, how am I supposed to get a nice picture? According to my question <a href="http://ask.sagemath.org/question/879/graph-editor-doesnt-workhttp://ask.sagemath.org/question/879/graph-editor-doesnt-work">here</a>, kcrisman says the graph editor is broken. That would have been a nice tool. My current strategy is to repeatedly have it show the graph and save it as a pdf until it gets into the format I want, i.e., run the following code over and over.</p>
<pre><code>graph_plot = g.plot(vertex_colors={'white'})
graph_plot.show()
graph_plot.save('graph.pdf')
</code></pre>
<p>But, this doesn't always end up with the picture I really want. And, occasionally I want to do something like delete a vertex and draw the graph again, but now all of the sudden the plot behaves completely differently and it never looks anything like the previous drawing minus one vertex.</p>
<p>So, what are some ways to get around this?</p>
http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?answer=14016#post-id-14016I often find that bumping up the number of iterations helps. For example,
sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot()
sage: p.save("g1.png")
gives
![misshapen](/upfiles/13471240534601034.png)
but
sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot(layout='spring', iterations=10000)
sage: p.save("g2.png")
gives
![nice](/upfiles/13471240991154882.png)
More generally, you can control the positions of the nodes by using a position dictionary, and you can save the current positions to start from. For example:
sage: p = g.plot(layout='spring', iterations=10000, save_pos=True)
sage: g.get_pos()
{0: [1.3716897101521048, -0.02830881780686272], 1: [1.211732997569143, 0.41733909879317593], 2: [1.0618077247691025, -0.5105153130614829], 3: [0.9519193694209523, 0.2027868664651152], 4: [1.674575908225974, -0.3855530203902292], 5: [1.8974214150303133, 0.20135568451141017], 6: [2.3146717824436642, -0.2518256884495827], 7: [1.6466675751821391, 0.7744402171620497], 8: [1.4420671784710155, -0.9954885089922162], 9: [0.7102586964467139, 0.7529303543692121], 10: [0.5362226032745113, -0.1771608726005901]}
And thus we can store and modify this information, so that
g = graphs.MycielskiGraph(4)
p = g.plot(layout='spring', iterations=10000, save_pos=True)
pos = (g.get_pos())
pos[10] = (2, 2)
g.delete_vertex(0)
p = g.plot(pos=pos, save_pos=True)
p.save("g3.png")
gave me
![image description](/upfiles/13471249569723123.png)
Sat, 08 Sep 2012 07:22:56 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?answer=14016#post-id-14016Comment by G-Sage for <p>I often find that bumping up the number of iterations helps. For example,</p>
<pre><code>sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot()
sage: p.save("g1.png")
</code></pre>
<p>gives</p>
<p><img alt="misshapen" src="/upfiles/13471240534601034.png"/></p>
<p>but </p>
<pre><code>sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot(layout='spring', iterations=10000)
sage: p.save("g2.png")
</code></pre>
<p>gives </p>
<p><img alt="nice" src="/upfiles/13471240991154882.png"/></p>
<p>More generally, you can control the positions of the nodes by using a position dictionary, and you can save the current positions to start from. For example:</p>
<pre><code>sage: p = g.plot(layout='spring', iterations=10000, save_pos=True)
sage: g.get_pos()
{0: [1.3716897101521048, -0.02830881780686272], 1: [1.211732997569143, 0.41733909879317593], 2: [1.0618077247691025, -0.5105153130614829], 3: [0.9519193694209523, 0.2027868664651152], 4: [1.674575908225974, -0.3855530203902292], 5: [1.8974214150303133, 0.20135568451141017], 6: [2.3146717824436642, -0.2518256884495827], 7: [1.6466675751821391, 0.7744402171620497], 8: [1.4420671784710155, -0.9954885089922162], 9: [0.7102586964467139, 0.7529303543692121], 10: [0.5362226032745113, -0.1771608726005901]}
</code></pre>
<p>And thus we can store and modify this information, so that</p>
<pre><code>g = graphs.MycielskiGraph(4)
p = g.plot(layout='spring', iterations=10000, save_pos=True)
pos = (g.get_pos())
pos[10] = (2, 2)
g.delete_vertex(0)
p = g.plot(pos=pos, save_pos=True)
p.save("g3.png")
</code></pre>
<p>gave me</p>
<p><img alt="image description" src="/upfiles/13471249569723123.png"/></p>
http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19092#post-id-19092This is great, thanks. I withhold accepting to see if there are other answers. The higher iterations definitely helped make the graph come in a nice format, though I still have to run it several times to get one that is rotated in the right direction. I could probably write up some code that would rotate all the points a number of degrees or radians and use that, using the save position stuff. Great answer.Sat, 08 Sep 2012 09:05:24 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19092#post-id-19092Comment by G-Sage for <p>I often find that bumping up the number of iterations helps. For example,</p>
<pre><code>sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot()
sage: p.save("g1.png")
</code></pre>
<p>gives</p>
<p><img alt="misshapen" src="/upfiles/13471240534601034.png"/></p>
<p>but </p>
<pre><code>sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot(layout='spring', iterations=10000)
sage: p.save("g2.png")
</code></pre>
<p>gives </p>
<p><img alt="nice" src="/upfiles/13471240991154882.png"/></p>
<p>More generally, you can control the positions of the nodes by using a position dictionary, and you can save the current positions to start from. For example:</p>
<pre><code>sage: p = g.plot(layout='spring', iterations=10000, save_pos=True)
sage: g.get_pos()
{0: [1.3716897101521048, -0.02830881780686272], 1: [1.211732997569143, 0.41733909879317593], 2: [1.0618077247691025, -0.5105153130614829], 3: [0.9519193694209523, 0.2027868664651152], 4: [1.674575908225974, -0.3855530203902292], 5: [1.8974214150303133, 0.20135568451141017], 6: [2.3146717824436642, -0.2518256884495827], 7: [1.6466675751821391, 0.7744402171620497], 8: [1.4420671784710155, -0.9954885089922162], 9: [0.7102586964467139, 0.7529303543692121], 10: [0.5362226032745113, -0.1771608726005901]}
</code></pre>
<p>And thus we can store and modify this information, so that</p>
<pre><code>g = graphs.MycielskiGraph(4)
p = g.plot(layout='spring', iterations=10000, save_pos=True)
pos = (g.get_pos())
pos[10] = (2, 2)
g.delete_vertex(0)
p = g.plot(pos=pos, save_pos=True)
p.save("g3.png")
</code></pre>
<p>gave me</p>
<p><img alt="image description" src="/upfiles/13471249569723123.png"/></p>
http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19077#post-id-19077I still think your answer is great, but it doesn't work as well as I thought when you delete certain vertices. I can use your pictures as an example, even though there are vertex deletions, because they show the same idea. Consider your second and third pictures. Even though you use the same positions for 9 of the 10 vertices, the part of the graph containing those 9 vertices gets much smaller from picture 2 to picture 3. This same thing happened when I deleted half the vertices in a graph and wanted to see the other half with the same positions. Instead, that half of the graph got much bigger and now I have to mess with LaTeX to try to pick the correct width for the pictures to end up having them look correct in my paper. Any ideas for this? Not that big a deal I guess.Mon, 10 Sep 2012 11:07:37 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19077#post-id-19077Comment by DSM for <p>I often find that bumping up the number of iterations helps. For example,</p>
<pre><code>sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot()
sage: p.save("g1.png")
</code></pre>
<p>gives</p>
<p><img alt="misshapen" src="/upfiles/13471240534601034.png"/></p>
<p>but </p>
<pre><code>sage: g = graphs.MycielskiGraph(4)
sage: p = g.plot(layout='spring', iterations=10000)
sage: p.save("g2.png")
</code></pre>
<p>gives </p>
<p><img alt="nice" src="/upfiles/13471240991154882.png"/></p>
<p>More generally, you can control the positions of the nodes by using a position dictionary, and you can save the current positions to start from. For example:</p>
<pre><code>sage: p = g.plot(layout='spring', iterations=10000, save_pos=True)
sage: g.get_pos()
{0: [1.3716897101521048, -0.02830881780686272], 1: [1.211732997569143, 0.41733909879317593], 2: [1.0618077247691025, -0.5105153130614829], 3: [0.9519193694209523, 0.2027868664651152], 4: [1.674575908225974, -0.3855530203902292], 5: [1.8974214150303133, 0.20135568451141017], 6: [2.3146717824436642, -0.2518256884495827], 7: [1.6466675751821391, 0.7744402171620497], 8: [1.4420671784710155, -0.9954885089922162], 9: [0.7102586964467139, 0.7529303543692121], 10: [0.5362226032745113, -0.1771608726005901]}
</code></pre>
<p>And thus we can store and modify this information, so that</p>
<pre><code>g = graphs.MycielskiGraph(4)
p = g.plot(layout='spring', iterations=10000, save_pos=True)
pos = (g.get_pos())
pos[10] = (2, 2)
g.delete_vertex(0)
p = g.plot(pos=pos, save_pos=True)
p.save("g3.png")
</code></pre>
<p>gave me</p>
<p><img alt="image description" src="/upfiles/13471249569723123.png"/></p>
http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19076#post-id-19076Unfortunately there aren't a lot of options between "almost no control, but automatic" and "complete control, but entirely manual". When needed, I experiment to get a place to start and then set the positions manually.Mon, 10 Sep 2012 11:24:06 -0500http://ask.sagemath.org/question/9305/strategies-for-drawing-good-graphs-graph-theory/?comment=19076#post-id-19076