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.Mon, 03 May 2021 00:00:04 +0200Plot "color_by_label" produces SQUARED number of lines in digraphhttps://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/I bumped into this unusual behavior and am not sure how to fix it.
When I add `color_by_label` for plotting a digraph I get the correct
number SQUARED of lines appearing in the digraph. Is this an error
in SageMath or is there a workaround? All monochrome lines
do not work for my application.
This is my code sample:
stnc = 'ABCCCCDABCDABCDA'
g = DiGraph({}, loops=True, multiedges=True)
for a, b in [(stnc[i], stnc[i + 1]) for i in range(len(stnc) - 1)]:
g.add_edge(a, b, b)
sage: g.edges()
[('A', 'B', 'B'), ('A', 'B', 'B'), ('A', 'B', 'B'),
('B', 'C', 'C'), ('B', 'C', 'C'), ('B', 'C', 'C'),
('C', 'C', 'C'), ('C', 'C', 'C'), ('C', 'C', 'C'),
('C', 'D', 'D'), ('C', 'D', 'D'), ('C', 'D', 'D'),
('D', 'A', 'A'), ('D', 'A', 'A'), ('D', 'A', 'A')]
# This produces the correct number of lines SQUARED
g.plot(color_by_label=True, edge_style='solid', layout='circular').show(figsize=(8, 8))
# Correct number of lines but monochrome
g.plot(layout='circular').show(figsize=(8, 8))
![Incorrect edge multiplicity for multidigraph with color_by_label](/upfiles/16164608644236614.png)
Each edge should have multiplicity 3.
In the plot with color edges, each has multiplicity 9.
The plot with no colors has correct multiplicity 3 for each edge.Tue, 23 Mar 2021 00:17:54 +0100https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/Comment by slelievre for <p>I bumped into this unusual behavior and am not sure how to fix it.
When I add <code>color_by_label</code> for plotting a digraph I get the correct
number SQUARED of lines appearing in the digraph. Is this an error
in SageMath or is there a workaround? All monochrome lines
do not work for my application.</p>
<p>This is my code sample:</p>
<pre><code>stnc = 'ABCCCCDABCDABCDA'
g = DiGraph({}, loops=True, multiedges=True)
for a, b in [(stnc[i], stnc[i + 1]) for i in range(len(stnc) - 1)]:
g.add_edge(a, b, b)
sage: g.edges()
[('A', 'B', 'B'), ('A', 'B', 'B'), ('A', 'B', 'B'),
('B', 'C', 'C'), ('B', 'C', 'C'), ('B', 'C', 'C'),
('C', 'C', 'C'), ('C', 'C', 'C'), ('C', 'C', 'C'),
('C', 'D', 'D'), ('C', 'D', 'D'), ('C', 'D', 'D'),
('D', 'A', 'A'), ('D', 'A', 'A'), ('D', 'A', 'A')]
# This produces the correct number of lines SQUARED
g.plot(color_by_label=True, edge_style='solid', layout='circular').show(figsize=(8, 8))
# Correct number of lines but monochrome
g.plot(layout='circular').show(figsize=(8, 8))
</code></pre>
<p><img src="/upfiles/16164608644236614.png" alt="Incorrect edge multiplicity for multidigraph with color_by_label"></p>
<p>Each edge should have multiplicity 3.
In the plot with color edges, each has multiplicity 9.
The plot with no colors has correct multiplicity 3 for each edge.</p>
https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/?comment=56911#post-id-56911I'm curious what the name "stnc" stands for... Could you explain?Mon, 03 May 2021 00:00:04 +0200https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/?comment=56911#post-id-56911Answer by slelievre for <p>I bumped into this unusual behavior and am not sure how to fix it.
When I add <code>color_by_label</code> for plotting a digraph I get the correct
number SQUARED of lines appearing in the digraph. Is this an error
in SageMath or is there a workaround? All monochrome lines
do not work for my application.</p>
<p>This is my code sample:</p>
<pre><code>stnc = 'ABCCCCDABCDABCDA'
g = DiGraph({}, loops=True, multiedges=True)
for a, b in [(stnc[i], stnc[i + 1]) for i in range(len(stnc) - 1)]:
g.add_edge(a, b, b)
sage: g.edges()
[('A', 'B', 'B'), ('A', 'B', 'B'), ('A', 'B', 'B'),
('B', 'C', 'C'), ('B', 'C', 'C'), ('B', 'C', 'C'),
('C', 'C', 'C'), ('C', 'C', 'C'), ('C', 'C', 'C'),
('C', 'D', 'D'), ('C', 'D', 'D'), ('C', 'D', 'D'),
('D', 'A', 'A'), ('D', 'A', 'A'), ('D', 'A', 'A')]
# This produces the correct number of lines SQUARED
g.plot(color_by_label=True, edge_style='solid', layout='circular').show(figsize=(8, 8))
# Correct number of lines but monochrome
g.plot(layout='circular').show(figsize=(8, 8))
</code></pre>
<p><img src="/upfiles/16164608644236614.png" alt="Incorrect edge multiplicity for multidigraph with color_by_label"></p>
<p>Each edge should have multiplicity 3.
In the plot with color edges, each has multiplicity 9.
The plot with no colors has correct multiplicity 3 for each edge.</p>
https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/?answer=56326#post-id-56326That is a bug. I think the faulty lines of code are
- [lines 665 to 671 in `src/sage/graphs/graph_plot.py`](https://github.com/sagemath/sage/blob/5ef2540/src/sage/graphs/graph_plot.py#L665-L671).
Fixing this is now tracked at
- [Sage Trac ticket 31542: Fix edge multiplicity in multidigraph plot with color_by_label](https://trac.sagemath.org/ticket/31542)
-----
Edit. The ticket now has fix and positive review.
Hopefully it will be part of the next Sage release.
If you installed Sage 9.2 or later from source or from binaries (rather
than "via a package manager"), you can apply the fix to your current
Sage installation by running the following commands in a terminal:
$ cd $(sage -c "print(SAGE_ROOT)")
$ git remote add trac git://trac.sagemath.org/sage.git -t develop
$ git checkout -b 31542
$ git pull trac u/chapoton/31542
$ ./sage -b
Next time you start Sage, it should have the fix.
![Correct plot of multiedge digraph](/upfiles/16165027435632568.png)Tue, 23 Mar 2021 01:37:04 +0100https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/?answer=56326#post-id-56326Comment by JJ for <p>That is a bug. I think the faulty lines of code are</p>
<ul>
<li><a href="https://github.com/sagemath/sage/blob/5ef2540/src/sage/graphs/graph_plot.py#L665-L671">lines 665 to 671 in <code>src/sage/graphs/graph_plot.py</code></a>.</li>
</ul>
<p>Fixing this is now tracked at</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/31542">Sage Trac ticket 31542: Fix edge multiplicity in multidigraph plot with color_by_label</a></li>
</ul>
<hr>
<p>Edit. The ticket now has fix and positive review.
Hopefully it will be part of the next Sage release.</p>
<p>If you installed Sage 9.2 or later from source or from binaries (rather
than "via a package manager"), you can apply the fix to your current
Sage installation by running the following commands in a terminal:</p>
<pre><code>$ cd $(sage -c "print(SAGE_ROOT)")
$ git remote add trac git://trac.sagemath.org/sage.git -t develop
$ git checkout -b 31542
$ git pull trac u/chapoton/31542
$ ./sage -b
</code></pre>
<p>Next time you start Sage, it should have the fix.</p>
<p><img src="/upfiles/16165027435632568.png" alt="Correct plot of multiedge digraph"></p>
https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/?comment=56349#post-id-56349Thank you so much for the excellent feedback and quick response. Based on your instructions I created a SabeMath 9.2 install from the source in a Linux virtual machine and then applied the patch. Everything worked fine.Wed, 24 Mar 2021 03:34:56 +0100https://ask.sagemath.org/question/56324/plot-color_by_label-produces-squared-number-of-lines-in-digraph/?comment=56349#post-id-56349