Ask Your Question

Coloring edges by their labels

asked 2012-09-28 10:39:41 -0500

mesri gravatar image

updated 2012-09-30 17:38:02 -0500

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

answered 2012-09-28 18:31:56 -0500

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.

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

This produced the following output. image description

edit flag offensive delete link more


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

kcrisman gravatar imagekcrisman ( 2012-09-29 15:02:10 -0500 )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 15:04:47 -0500 )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-09-30 17:09:31 -0500 )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-09-30 17:35:44 -0500 )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 02:45:18 -0500 )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


Asked: 2012-09-28 10:39:41 -0500

Seen: 659 times

Last updated: Sep 28 '12