0

I have a graph where each edge (not nodes) has a capacity and a load. I want to draw this graph using a color map which is a gradient from blue to red, red being the most loaded and blue being the less loaded. I tried to understand the official documentation of Edge Colormap, but it doesn't help me. Can you help me?

Xia
  • 156
  • 2
  • 9

1 Answers1

0

The key is to set edge_color to be a sequence of edges coinciding with edgelist, both keyword arguments of nx.draw_networkx. Here is a minimal working example using pandas and networkx.

import networkx as nx
import pandas as pd

edge_df = pd.DataFrame({"source": [0, 1, 2],
                       "target": [1, 2, 0],
                       "capacity": [.1, .2, .3],
                       "load": [40, 20, 10]})

G = nx.from_pandas_edgelist(edge_df,
                            source="source",
                            target="target",
                            edge_attr=["capacity", "load"])

nx.draw_networkx(G,
                 edgelist=list(zip(edge_df['source'], edge_df['target'])),
                 edge_color=edge_df['capacity'],
                 edge_cmap=plt.cm.bwr)

Should get you something like this:

enter image description here

rodgdor
  • 2,530
  • 1
  • 19
  • 26