Networkx: Use a different color for each connected component
<p>I am using networkx. I mean to draw a directed graph using a different color for each of the <a href="https://networkx.github.io/documentation/networkx-1.9.1/reference/generated/networkx.algorithms.components.weakly_connected.weakly_connected_component_subgraphs.html" rel="nofollow">weakly_connected_component_subgraphs</a>.</p>

<p>This means using that color for both nodes and edges of the subgraph.
I followed the example <a href="https://networkx.github.io/documentation/networkx-1.10/examples/drawing/weighted_graph.html" rel="nofollow">here</a>,
adding a list of colors, one per node, as the parameter of <code>node_color</code> in <code>nx.draw_networkx_nodes</code>.</p>

<p><strong>The list of colors for nodes</strong> was built with</p>

<pre><code>colorlist = [ 'r', 'g', 'b', 'c', 'm', 'y', 'k' ]
ncolors = len( colorlist )
    wcc = nx.weakly_connected_component_subgraphs( my_network )
    color_nodes = [ colorlist[ 0 ] ] * nnodes
    isg = 0
    for sg in wcc :
        isg += 1
        color_sg = colorlist[ isg % ncolors ]
        # List of nodes in sg
        sgnodes = nx.nodes(sg)
        for i in range( len( sgnodes ) ) :
            color_nodes[ sgnodes[ i ] ] = color_sg
</code></pre>

<p>I took advantage of my nodes being labeled with a sequential numbering, starting from zero.</p>

<p><strong>How can I set the list of colors for the edges?</strong> (perhaps the same issue would arise for nodes if they were not sequentially numbered).</p>

<p>I conceieve a way that gets    <code>sgedges = nx.edges(sg)</code>
and then iterates over         <code>nwedges = nx.edges(my_network)</code> and uses a counter, checking if each edge is in <code>sgedges</code>. If so, set the corresponding element in a list     <code>color_edges = [ colorlist[ 0 ] ] * nedges</code>.</p>

<p>This seems to me quite convoluted, and I guess there might be a simpler way.</p>