ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 21 Apr 2019 13:04:48 +0200creating graph with dotted edgeshttps://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/ Hello all,
I need to draw a graph in which some edges are usual and some edges are dotted. I know how to make all the edges dotted (using the option "dotted" for the parameter edge_style of plot). But I need mixed edges (some are dotted and some are usual). Kindly help me with this.
Thank you.
Mon, 08 Oct 2018 18:17:43 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/Comment by slelievre for <p>Hello all,
I need to draw a graph in which some edges are usual and some edges are dotted. I know how to make all the edges dotted (using the option "dotted" for the parameter edge_style of plot). But I need mixed edges (some are dotted and some are usual). Kindly help me with this.
Thank you.</p>
https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=44046#post-id-44046Note: also asked as
- [Math Stack Exchange question 2947293: plotting graph with dotted edges](https://math.stackexchange.com/q/2947293)Wed, 24 Oct 2018 00:20:02 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=44046#post-id-44046Comment by slelievre for <p>Hello all,
I need to draw a graph in which some edges are usual and some edges are dotted. I know how to make all the edges dotted (using the option "dotted" for the parameter edge_style of plot). But I need mixed edges (some are dotted and some are usual). Kindly help me with this.
Thank you.</p>
https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=46297#post-id-46297See also
- [Stack Overflow question 55768473: How to color an edge in a graph as a certain color](https://stackoverflow.com/questions/55768473)Sun, 21 Apr 2019 12:35:01 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=46297#post-id-46297Answer by tmonteil for <p>Hello all,
I need to draw a graph in which some edges are usual and some edges are dotted. I know how to make all the edges dotted (using the option "dotted" for the parameter edge_style of plot). But I need mixed edges (some are dotted and some are usual). Kindly help me with this.
Thank you.</p>
https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?answer=43874#post-id-43874Let me pick a graph:
sage: G = graphs.RandomGNP(10, 1/2)
Together with two subsets of edges to be drawn differently:
sage: dotted_edges = G.edges()[:10]
sage: solid_edges = G.edges()[10:]
First, note that there is a simple way to *color* the edges according to the previous partition:
sage: G.plot(edge_colors = {'red': dotted_edges, 'green': solid_edges})
However, setting the edge_style in a non-uniform way is currently not possible (though it could be a good feature to request). A possible workaround is the following: we can build two graphs corresponding to the two (or more) subsets of edges, and add the plots:
sage: Graph(dotted_edges).plot(edge_style="dotted") + Graph(solid_edges).plot(edge_style="solid")
As you can see, this does not work correctly, since the position of the vertices is computed twice hence the vertices of the second graph are not at the same position than the corresponding vertices of the first graph. So what we have to do is to save the positions of some plot, and then to use them in the requested plot:
sage: G.plot(save_pos=True)
sage: position = G.get_pos()
sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid")
**EDIT** If you do not like axes, you can add `axes=False` to one of the plots:
sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid", axes=False)Mon, 08 Oct 2018 20:41:48 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?answer=43874#post-id-43874Comment by GA316 for <p>Let me pick a graph:</p>
<pre><code>sage: G = graphs.RandomGNP(10, 1/2)
</code></pre>
<p>Together with two subsets of edges to be drawn differently:</p>
<pre><code>sage: dotted_edges = G.edges()[:10]
sage: solid_edges = G.edges()[10:]
</code></pre>
<p>First, note that there is a simple way to <em>color</em> the edges according to the previous partition:</p>
<pre><code>sage: G.plot(edge_colors = {'red': dotted_edges, 'green': solid_edges})
</code></pre>
<p>However, setting the edge_style in a non-uniform way is currently not possible (though it could be a good feature to request). A possible workaround is the following: we can build two graphs corresponding to the two (or more) subsets of edges, and add the plots:</p>
<pre><code>sage: Graph(dotted_edges).plot(edge_style="dotted") + Graph(solid_edges).plot(edge_style="solid")
</code></pre>
<p>As you can see, this does not work correctly, since the position of the vertices is computed twice hence the vertices of the second graph are not at the same position than the corresponding vertices of the first graph. So what we have to do is to save the positions of some plot, and then to use them in the requested plot:</p>
<pre><code>sage: G.plot(save_pos=True)
sage: position = G.get_pos()
sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid")
</code></pre>
<p><strong>EDIT</strong> If you do not like axes, you can add <code>axes=False</code> to one of the plots:</p>
<pre><code>sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid", axes=False)
</code></pre>
https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=43890#post-id-43890Thanks. It works nicely. Can you please tell me how to add labels only on certain edges? thank you.Wed, 10 Oct 2018 08:03:52 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=43890#post-id-43890Comment by GA316 for <p>Let me pick a graph:</p>
<pre><code>sage: G = graphs.RandomGNP(10, 1/2)
</code></pre>
<p>Together with two subsets of edges to be drawn differently:</p>
<pre><code>sage: dotted_edges = G.edges()[:10]
sage: solid_edges = G.edges()[10:]
</code></pre>
<p>First, note that there is a simple way to <em>color</em> the edges according to the previous partition:</p>
<pre><code>sage: G.plot(edge_colors = {'red': dotted_edges, 'green': solid_edges})
</code></pre>
<p>However, setting the edge_style in a non-uniform way is currently not possible (though it could be a good feature to request). A possible workaround is the following: we can build two graphs corresponding to the two (or more) subsets of edges, and add the plots:</p>
<pre><code>sage: Graph(dotted_edges).plot(edge_style="dotted") + Graph(solid_edges).plot(edge_style="solid")
</code></pre>
<p>As you can see, this does not work correctly, since the position of the vertices is computed twice hence the vertices of the second graph are not at the same position than the corresponding vertices of the first graph. So what we have to do is to save the positions of some plot, and then to use them in the requested plot:</p>
<pre><code>sage: G.plot(save_pos=True)
sage: position = G.get_pos()
sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid")
</code></pre>
<p><strong>EDIT</strong> If you do not like axes, you can add <code>axes=False</code> to one of the plots:</p>
<pre><code>sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid", axes=False)
</code></pre>
https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=43891#post-id-43891Also in the output graph with dotted edges and ordinary edges, I am getting axes also part of the output. But I need only the graph in order to add it in my paper. Kindly tell me how to get rid of this measurement axes. Thank yu.Wed, 10 Oct 2018 08:17:11 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=43891#post-id-43891Comment by slelievre for <p>Let me pick a graph:</p>
<pre><code>sage: G = graphs.RandomGNP(10, 1/2)
</code></pre>
<p>Together with two subsets of edges to be drawn differently:</p>
<pre><code>sage: dotted_edges = G.edges()[:10]
sage: solid_edges = G.edges()[10:]
</code></pre>
<p>First, note that there is a simple way to <em>color</em> the edges according to the previous partition:</p>
<pre><code>sage: G.plot(edge_colors = {'red': dotted_edges, 'green': solid_edges})
</code></pre>
<p>However, setting the edge_style in a non-uniform way is currently not possible (though it could be a good feature to request). A possible workaround is the following: we can build two graphs corresponding to the two (or more) subsets of edges, and add the plots:</p>
<pre><code>sage: Graph(dotted_edges).plot(edge_style="dotted") + Graph(solid_edges).plot(edge_style="solid")
</code></pre>
<p>As you can see, this does not work correctly, since the position of the vertices is computed twice hence the vertices of the second graph are not at the same position than the corresponding vertices of the first graph. So what we have to do is to save the positions of some plot, and then to use them in the requested plot:</p>
<pre><code>sage: G.plot(save_pos=True)
sage: position = G.get_pos()
sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid")
</code></pre>
<p><strong>EDIT</strong> If you do not like axes, you can add <code>axes=False</code> to one of the plots:</p>
<pre><code>sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid", axes=False)
</code></pre>
https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=46298#post-id-46298@GA316: what do you mean by "add labels only on certain edges"?Sun, 21 Apr 2019 13:04:48 +0200https://ask.sagemath.org/question/43872/creating-graph-with-dotted-edges/?comment=46298#post-id-46298