Difficulty coloring edges

1,009 views
Skip to first unread message

Ashley

unread,
Nov 23, 2009, 12:39:05 PM11/23/09
to networkx-discuss
Hello!

I've been trying to figure out how to plot a graph, with each edge
colored differently. However, I am plagued by the following error
message:

ValueError: edge_color must be a single color or list of exactly m
colors where m is the number or edges

So, I've tried a bunch of things out, including

edgecolor=[]
for i in G.edges():
edgecolor.append('r')

But, this doesn't work. It throws the same error. Wouldn't this
create a list of exactly "m" edges?

Trying G.add_edge(edge1, edge2, color(or edge_color)='r') doesn't help
at all either. When I render the edges, they are all black. Is there
another variable I should be passing here?

Anyhow, I'm loading nodes from a file, and then edges from that file,
feeding them into NetworkX the entire time. I'd like to color the
edges depending on the nodes they connect. Does anyone have any
ideas? Coloring the nodes and changing edge width works just fine,
but I'd like to color the edges.

I'd really appreciate any help. I'm not that great with Python, so
please keep that in mind :)

Aric Hagberg

unread,
Nov 23, 2009, 8:34:57 PM11/23/09
to networkx...@googlegroups.com
On Mon, Nov 23, 2009 at 10:39 AM, Ashley <ashleyd...@gmail.com> wrote:
> Hello!
>
> I've been trying to figure out how to plot a graph, with each edge
> colored differently.  However, I am plagued by the following error
> message:
>
> ValueError: edge_color must be a single color or list of exactly m
> colors where m is the number or edges
>
> So, I've tried a bunch of things out, including
>
> edgecolor=[]
> for i in G.edges():
>    edgecolor.append('r')
>
> But, this doesn't work.  It throws the same error.  Wouldn't this
> create a list of exactly "m" edges?
>

Something like this should work:

import networkx as nx
import pylab
G=nx.path_graph(4)
nx.draw(G,edge_color=['r','g','b'],width=4)
pylab.show()


Also see the example:

http://networkx.lanl.gov/examples/drawing/edge_colormap.html

Aric

Ashley

unread,
Nov 27, 2009, 1:39:50 PM11/27/09
to networkx-discuss
Those examples work for me, but when I try it with a more complicated
graph, they do not.

Here is the core of my code:

edgecolor=[]

for line in file:
s= line.split()
if(s[0]=="NODE"):
G.add_node(s[1])
if(s[0]=="EDGE"):
G.add_edge(s[1], s[2])
edgecolor.append('b')

nx.draw(G, edge_color=edgecolor, node_size=10.0, with_labels = False)

Even if I try

for line in file:
s= line.split()
if(s[0]=="NODE"):
G.add_node(s[1])
if(s[0]=="EDGE"):
G.add_edge(s[1], s[2])

for i in G.edges():
edgecolor.append('r')

It still doesn't work.

My file is formatted like:

NODE 1
NODE 2
...
NODE 6559
NODE 6560
EDGE 4 2651
EDGE 6 6528
EDGE 6 6529
...
EDGE 6554 4789
EDGE 6554 4790

There are no nodes with edges to themselves.

I get the same error:

ValueError: edge_color must be a single color or list of exactly m
colors where m is the number or edges

I'm probably missing something obvious in here, but I haven't been
able to solve it, at all. I'd really appreciate any help in solving
this problem... if you need any more information, please let me know.

On Nov 23, 7:34 pm, Aric Hagberg <ahagb...@gmail.com> wrote:

Ashley

unread,
Nov 27, 2009, 1:43:31 PM11/27/09
to networkx-discuss
Also, just to emphasize -- I know that the code I have right now would
set every edge to the same color if it works. I figure once I have
that working, I'll add in cases to color the edges differently
depending on where they connect. So, regardless of how my code works
now, I really do want different edges to have different colors :)
Reply all
Reply to author
Forward
0 new messages