On 04/27/2010 12:41 PM, Rajeev wrote:
Thanks Jason,
But the color assigned is random. I wonder if it is possible to assign
it systematically (like image in matplotlib) and possibly with a
colorbar.
I think you'll have to do that by manually assigning edge colors.
Here's an example. If you know how to use the colormap tools from
matplotlib, you can use them as well.
sage: g=DiGraph(random_matrix(ZZ,5),weighted=True)
sage: g
Looped digraph on 5 vertices
sage: g.weighted_adjacency_matrix()
[-1 2 -2 -1 -1]
[ 2 -9 1 2 3]
[-1 -3 -1 -1 1]
[ 6 -1 -4 4 1]
[-1 -1 6 -3 -4]
Now define a function that takes in a weight and returns a triple
representing red, green, and blue components of the color
sage: def edge_color(weight):
....: return (weight/10,weight/10,weight/10)
....:
sage: g.edges()
[(0, 0, -1), (0, 1, 2), (0, 2, -2), (0, 3, -1), (0, 4, -1), (1, 0, 2),
(1, 1, -9), (1, 2, 1), (1, 3, 2), (1, 4, 3), (2, 0, -1), (2, 1, -3), (2,
2, -1), (2, 3, -1), (2, 4, 1), (3, 0, 6), (3, 1, -1), (3, 2, -4), (3, 3,
4), (3, 4, 1), (4, 0, -1), (4, 1, -1), (4, 2, 6), (4, 3, -3), (4, 4, -4)]
sage: edge_colors=dict()
sage: for i,j,weight in g.edges():
....: edge_colors[edge_color(weight)] =
edge_colors.get(edge_color(weight),[])+[(i,j)]
sage: edge_colors
{(-3/10, -3/10, -3/10): [(2, 1), (4, 3)], (-1/5, -1/5, -1/5): [(0, 2)],
(3/5, 3/5, 3/5): [(3, 0), (4, 2)], (1/5, 1/5, 1/5): [(0, 1), (1, 0), (1,
3)], (-2/5, -2/5, -2/5): [(3, 2), (4, 4)], (3/10, 3/10, 3/10): [(1, 4)],
(2/5, 2/5, 2/5): [(3, 3)], (-9/10, -9/10, -9/10): [(1, 1)], (1/10, 1/10,
1/10): [(1, 2), (2, 4), (3, 4)], (-1/10, -1/10, -1/10): [(0, 0), (0, 3),
(0, 4), (2, 0), (2, 2), (2, 3), (3, 1), (4, 0), (4, 1)]}
sage: g.plot(edge_colors=edge_colors)
Or using a matplotlib colormap:
def edge_color(weight):
return sage.plot.colors.colormaps.Blues(weight/10)[0:3]
Thanks,
Jason
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org