Ask Your Question
0

Coloring edges by their labels

asked 2012-09-28 17:39:41 +0100

mesri gravatar image

updated 2012-10-01 00:38:02 +0100

fidbc gravatar image

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

version. Sage 5.0.1 on Mac OS X 10.8.1, notebook running in Chrome

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2012-09-29 01:31:56 +0100

fidbc gravatar image

It 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

edit flag offensive delete link more

Comments

Nice - do you think we should change `color_by_label` to allow customization more easily?

kcrisman gravatar imagekcrisman ( 2012-09-29 22:02:10 +0100 )edit

Thanks! 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 ?

mesri gravatar imagemesri ( 2012-09-30 22:04:47 +0100 )edit

@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.

fidbc gravatar imagefidbc ( 2012-10-01 00:09:31 +0100 )edit

@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.

fidbc gravatar imagefidbc ( 2012-10-01 00:35:44 +0100 )edit

@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.

kcrisman gravatar imagekcrisman ( 2012-10-01 09:45:18 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-09-28 17:39:41 +0100

Seen: 2,476 times

Last updated: Sep 29 '12