Ask Your Question
0

Coloring edges by their labels

asked 12 years ago

mesri gravatar image

updated 12 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 12 years ago

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

Preview: (hide)
link

Comments

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

kcrisman gravatar imagekcrisman ( 12 years ago )

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 ( 12 years ago )

@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 ( 12 years ago )

@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 ( 12 years ago )

@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 ( 12 years ago )

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: 12 years ago

Seen: 2,788 times

Last updated: Sep 29 '12