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, 01 Oct 2012 02:45:18 -0500Coloring edges by their labelshttp://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/I'm trying to understand how to use the color_by_label parameter of the Graph.plot() function to set edge colors depending on various conditions. I cannot seem to get any colors to display other than cyan and red, no matter what I put in the edge labels, and, as in the linked image, I cannot seem to reliably get the colors to go with the labels.
In my example, I was trying to set edge color to blue if either of the vertices of an edge is odd, red otherwise.
(Sorry for the external link -- I apparently need to acquire some karma before I can post it here)
[Screen Shot of sage notebook session and graphics output](http://tinypic.com/r/ou5hxt/6)
version. Sage 5.0.1 on Mac OS X 10.8.1, notebook running in ChromeFri, 28 Sep 2012 10:39:41 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/Answer by fidbc for <p>I'm trying to understand how to use the color_by_label parameter of the Graph.plot() function to set edge colors depending on various conditions. I cannot seem to get any colors to display other than cyan and red, no matter what I put in the edge labels, and, as in the linked image, I cannot seem to reliably get the colors to go with the labels.</p>
<p>In my example, I was trying to set edge color to blue if either of the vertices of an edge is odd, red otherwise.</p>
<p>(Sorry for the external link -- I apparently need to acquire some karma before I can post it here)</p>
<p><a href="http://tinypic.com/r/ou5hxt/6">Screen Shot of sage notebook session and graphics output</a></p>
<p>version. Sage 5.0.1 on Mac OS X 10.8.1, notebook running in Chrome</p>
http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?answer=14076#post-id-14076It seems that the `color_by_label` option from the `plot` function just uses as many colors as labels from the `sage.plot.colors.rainbow` function. That is, the color is not taken from the labels themselves but from the `rainbow` function.
Maybe the following example can help you obtain the plot settings you want.
g=graphs.IcosahedralGraph()
for e in g.edges():
g.set_edge_label(e[0],e[1],1 if e[0]%2 or e[1]%2 else 2)
d={1: "blue", 2: "green"} # _color_by_label will use this to map labels to colors
g.plot(edge_colors=g._color_by_label(d))
This produced the following output.
![image description](/upfiles/13488928619667999.png)Fri, 28 Sep 2012 18:31:56 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?answer=14076#post-id-14076Comment by mesri for <p>It seems that the <code>color_by_label</code> option from the <code>plot</code> function just uses as many colors as labels from the <code>sage.plot.colors.rainbow</code> function. That is, the color is not taken from the labels themselves but from the <code>rainbow</code> function.</p>
<p>Maybe the following example can help you obtain the plot settings you want.</p>
<pre><code>g=graphs.IcosahedralGraph()
for e in g.edges():
g.set_edge_label(e[0],e[1],1 if e[0]%2 or e[1]%2 else 2)
d={1: "blue", 2: "green"} # _color_by_label will use this to map labels to colors
g.plot(edge_colors=g._color_by_label(d))
</code></pre>
<p>This produced the following output.
<img alt="image description" src="/upfiles/13488928619667999.png"/></p>
http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18960#post-id-18960Thanks! That does it. So what is the relationship between the parameters edge_color, color_by_label, edge_colors, and this function graph._color_by_label ?Sun, 30 Sep 2012 15:04:47 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18960#post-id-18960Comment by fidbc for <p>It seems that the <code>color_by_label</code> option from the <code>plot</code> function just uses as many colors as labels from the <code>sage.plot.colors.rainbow</code> function. That is, the color is not taken from the labels themselves but from the <code>rainbow</code> function.</p>
<p>Maybe the following example can help you obtain the plot settings you want.</p>
<pre><code>g=graphs.IcosahedralGraph()
for e in g.edges():
g.set_edge_label(e[0],e[1],1 if e[0]%2 or e[1]%2 else 2)
d={1: "blue", 2: "green"} # _color_by_label will use this to map labels to colors
g.plot(edge_colors=g._color_by_label(d))
</code></pre>
<p>This produced the following output.
<img alt="image description" src="/upfiles/13488928619667999.png"/></p>
http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18959#post-id-18959@kcrisman: Yes it would be nice to be able to read edge colours from labels. So either your follow suggestion or at least point out in the documentation for `plot` how to use the `_color_by_label` workaround. This might be a good question for sage-devel to get some more input.Sun, 30 Sep 2012 17:09:31 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18959#post-id-18959Comment by fidbc for <p>It seems that the <code>color_by_label</code> option from the <code>plot</code> function just uses as many colors as labels from the <code>sage.plot.colors.rainbow</code> function. That is, the color is not taken from the labels themselves but from the <code>rainbow</code> function.</p>
<p>Maybe the following example can help you obtain the plot settings you want.</p>
<pre><code>g=graphs.IcosahedralGraph()
for e in g.edges():
g.set_edge_label(e[0],e[1],1 if e[0]%2 or e[1]%2 else 2)
d={1: "blue", 2: "green"} # _color_by_label will use this to map labels to colors
g.plot(edge_colors=g._color_by_label(d))
</code></pre>
<p>This produced the following output.
<img alt="image description" src="/upfiles/13488928619667999.png"/></p>
http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18958#post-id-18958@mesri: What happens when you call `g.plot(color_by_label=True)` is that the `plot` function ends up using the output of `g._color_by_label()` to as colors for the edges. You can think of this being equivalent to calling `g.plot(edge_colors=g._color_by_label())`. You can also see the documentation for `_color_by_label` for some more details about how colors are assigned to edges.Sun, 30 Sep 2012 17:35:44 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18958#post-id-18958Comment by kcrisman for <p>It seems that the <code>color_by_label</code> option from the <code>plot</code> function just uses as many colors as labels from the <code>sage.plot.colors.rainbow</code> function. That is, the color is not taken from the labels themselves but from the <code>rainbow</code> function.</p>
<p>Maybe the following example can help you obtain the plot settings you want.</p>
<pre><code>g=graphs.IcosahedralGraph()
for e in g.edges():
g.set_edge_label(e[0],e[1],1 if e[0]%2 or e[1]%2 else 2)
d={1: "blue", 2: "green"} # _color_by_label will use this to map labels to colors
g.plot(edge_colors=g._color_by_label(d))
</code></pre>
<p>This produced the following output.
<img alt="image description" src="/upfiles/13488928619667999.png"/></p>
http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18972#post-id-18972Nice - do you think we should change `color_by_label` to allow customization more easily?Sat, 29 Sep 2012 15:02:10 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18972#post-id-18972Comment by kcrisman for <p>It seems that the <code>color_by_label</code> option from the <code>plot</code> function just uses as many colors as labels from the <code>sage.plot.colors.rainbow</code> function. That is, the color is not taken from the labels themselves but from the <code>rainbow</code> function.</p>
<p>Maybe the following example can help you obtain the plot settings you want.</p>
<pre><code>g=graphs.IcosahedralGraph()
for e in g.edges():
g.set_edge_label(e[0],e[1],1 if e[0]%2 or e[1]%2 else 2)
d={1: "blue", 2: "green"} # _color_by_label will use this to map labels to colors
g.plot(edge_colors=g._color_by_label(d))
</code></pre>
<p>This produced the following output.
<img alt="image description" src="/upfiles/13488928619667999.png"/></p>
http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18951#post-id-18951@fidelbc: We really shouldn't have end users using underscored methods, so I would suggest raising that question on sage-devel as you recommend - graph theorists, and those who use graphs, are most likely to care.Mon, 01 Oct 2012 02:45:18 -0500http://ask.sagemath.org/question/9364/coloring-edges-by-their-labels/?comment=18951#post-id-18951