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.Sun, 13 Oct 2019 05:43:28 -0500How to draw the following graph in sagemath?http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/ How to draw the following graph in sagemath?
I am uploading the image here:
https://imgur.com/Q3sUXn8
I know how to draw a graph but I am finding it difficult to draw the graph in exactly the same way as shown in the picture.
Is it possible to do this?
I will be very grateful if someone could kindly help me outWed, 09 Oct 2019 07:27:58 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/Comment by dazedANDconfused for <p>How to draw the following graph in sagemath?</p>
<p>I am uploading the image here:</p>
<p><a href="https://imgur.com/Q3sUXn8">https://imgur.com/Q3sUXn8</a></p>
<p>I know how to draw a graph but I am finding it difficult to draw the graph in exactly the same way as shown in the picture.</p>
<p>Is it possible to do this?</p>
<p>I will be very grateful if someone could kindly help me out</p>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48248#post-id-48248It looks like you've asked the same question on LaTeX Stack Exchange [here](https://tex.stackexchange.com/questions/511582/how-to-draw-the-given-graph-in-latex). LaTeX, through the tkz-berge package, is probably the best way to specify the position to create a diagram.Wed, 09 Oct 2019 10:15:14 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48248#post-id-48248Answer by tmonteil for <p>How to draw the following graph in sagemath?</p>
<p>I am uploading the image here:</p>
<p><a href="https://imgur.com/Q3sUXn8">https://imgur.com/Q3sUXn8</a></p>
<p>I know how to draw a graph but I am finding it difficult to draw the graph in exactly the same way as shown in the picture.</p>
<p>Is it possible to do this?</p>
<p>I will be very grateful if someone could kindly help me out</p>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?answer=48247#post-id-48247If G is you graph, you can define coordinates of the vertices by using the `set_pos` method, see:
sage: G.set_pos?
for more details.
**EDIT** Having a look at the `set_pos` documentation, it is actually lacking meaningful examples, so here is how to use `set_pos`: you have to define a dictionary that associates to each vertex a coordinate (i.e. a tuple of two numbers), as follows:
sage: G = Graph(3)
sage: G.set_pos({0:(0,0), 1:(1,1), 2:(0,1)})
sage: G.plot()Wed, 09 Oct 2019 10:02:56 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?answer=48247#post-id-48247Comment by tmonteil for <p>If G is you graph, you can define coordinates of the vertices by using the <code>set_pos</code> method, see:</p>
<pre><code>sage: G.set_pos?
</code></pre>
<p>for more details.</p>
<p><strong>EDIT</strong> Having a look at the <code>set_pos</code> documentation, it is actually lacking meaningful examples, so here is how to use <code>set_pos</code>: you have to define a dictionary that associates to each vertex a coordinate (i.e. a tuple of two numbers), as follows:</p>
<pre><code>sage: G = Graph(3)
sage: G.set_pos({0:(0,0), 1:(1,1), 2:(0,1)})
sage: G.plot()
</code></pre>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48254#post-id-48254I consider Python/Sagemath a better programming language than LaTeX, so it is easier to produce a graph with Sage and then convert it into latex/tikz, with:
sage: latex(G)
This is especially true for complex graphs.Thu, 10 Oct 2019 04:27:44 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48254#post-id-48254Comment by Emmanuel Charpentier for <p>If G is you graph, you can define coordinates of the vertices by using the <code>set_pos</code> method, see:</p>
<pre><code>sage: G.set_pos?
</code></pre>
<p>for more details.</p>
<p><strong>EDIT</strong> Having a look at the <code>set_pos</code> documentation, it is actually lacking meaningful examples, so here is how to use <code>set_pos</code>: you have to define a dictionary that associates to each vertex a coordinate (i.e. a tuple of two numbers), as follows:</p>
<pre><code>sage: G = Graph(3)
sage: G.set_pos({0:(0,0), 1:(1,1), 2:(0,1)})
sage: G.plot()
</code></pre>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48317#post-id-48317Thierry, that's exactly what I thought of ; but my expresson wasn't exactly precise...Sun, 13 Oct 2019 05:43:28 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48317#post-id-48317Comment by Emmanuel Charpentier for <p>If G is you graph, you can define coordinates of the vertices by using the <code>set_pos</code> method, see:</p>
<pre><code>sage: G.set_pos?
</code></pre>
<p>for more details.</p>
<p><strong>EDIT</strong> Having a look at the <code>set_pos</code> documentation, it is actually lacking meaningful examples, so here is how to use <code>set_pos</code>: you have to define a dictionary that associates to each vertex a coordinate (i.e. a tuple of two numbers), as follows:</p>
<pre><code>sage: G = Graph(3)
sage: G.set_pos({0:(0,0), 1:(1,1), 2:(0,1)})
sage: G.plot()
</code></pre>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48250#post-id-48250A better answer can be obtained via a clever use of $\LaTeX$ and `Graphviz`. See the relevant [documentation](doc.sagemahttp://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph_latex.htmlth.org/html/en/reference/graphs/sage/graphs/graph_latex.html).Wed, 09 Oct 2019 14:03:04 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48250#post-id-48250Answer by Emmanuel Charpentier for <p>How to draw the following graph in sagemath?</p>
<p>I am uploading the image here:</p>
<p><a href="https://imgur.com/Q3sUXn8">https://imgur.com/Q3sUXn8</a></p>
<p>I know how to draw a graph but I am finding it difficult to draw the graph in exactly the same way as shown in the picture.</p>
<p>Is it possible to do this?</p>
<p>I will be very grateful if someone could kindly help me out</p>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?answer=48249#post-id-48249Alternate solution:
sage: G=Graph([[0,u] for u in (1..7)])
sage: G.add_edges([[u,4] for u in (1..3)])
sage: G.add_edges([[u,4] for u in (5..7)])
sage: D={0:[4,2]}
sage: D.update({4:[4,0]})
sage: for u in (1..3): D.update({u:[u,1]})
sage: for u in (5..7): D.update({u:[u,1]})
sage: G.plot(pos=D)
![The required graph (approximately...)](/upfiles/15706457902335247.png)
**EDIT :** If you insist on the order of the nodes:
sage: Dpos=[4, 3, 5, 2, 4, 6, 1, 7]
sage: G.plot(pos={u:[Dpos[u], 1+(u==0)-(u==4)] for u in (0..7)})
![A better approximation](/upfiles/15706473298725409.png)Wed, 09 Oct 2019 13:30:23 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?answer=48249#post-id-48249Comment by tmonteil for <p>Alternate solution:</p>
<pre><code>sage: G=Graph([[0,u] for u in (1..7)])
sage: G.add_edges([[u,4] for u in (1..3)])
sage: G.add_edges([[u,4] for u in (5..7)])
sage: D={0:[4,2]}
sage: D.update({4:[4,0]})
sage: for u in (1..3): D.update({u:[u,1]})
sage: for u in (5..7): D.update({u:[u,1]})
sage: G.plot(pos=D)
</code></pre>
<p><img alt="The required graph (approximately...)" src="/upfiles/15706457902335247.png"></p>
<p><strong>EDIT :</strong> If you insist on the order of the nodes:</p>
<pre><code>sage: Dpos=[4, 3, 5, 2, 4, 6, 1, 7]
sage: G.plot(pos={u:[Dpos[u], 1+(u==0)-(u==4)] for u in (0..7)})
</code></pre>
<p><img alt="A better approximation" src="/upfiles/15706473298725409.png"></p>
http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48252#post-id-48252You define the same `pos` dictionary to provide coordinates to the vertices, how is it alternate ?Wed, 09 Oct 2019 15:44:58 -0500http://ask.sagemath.org/question/48245/how-to-draw-the-following-graph-in-sagemath/?comment=48252#post-id-48252