I want to color edges of networks using Networkx and Matplotlib, where each edge (i,j)
is given a value G[i][j]['label']
between 0 and 1.
However, often, these values are either very close to zero, or very close to 1. It's then difficult to visualize the variations of color, since everything is either very red or very blue (using a coolwarm
colormap).
Then, my idea is to apply a filter filtR
like one of these ones :
It's simply a polynomial function which provides a bijection from [0,1] to [0,1], and stretches more values around 0 or 1. If needed, the inverse is tractable.
For now, I just apply it to the value of the edge, in order to define its color :
cm = plt.get_cmap('coolwarm')
cNorm = colors.Normalize(vmin=0., vmax=1.)
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
colorList = []
# The color is defined by filtR(G[i][j]['label'])
val_map = {(i,j): filtR(G[i][j]['label']) for (i,j) in G.edges()}
values = [scalarMap.to_rgba(val_map[e]) for e in G.edges()]
edges = nx.draw_networkx_edges(G,edge_color=values,edge_cmap=plt.get_cmap('coolwarm'))
# Definition of the colorbar :-(
sm = cmx.ScalarMappable(cmap=cmx.coolwarm)
sm.set_array(values)
plt.colorbar(sm)
The question is now : I would like to define the corresponding colorbar.
For now, it shows the evaluation of my edges by the filtR
function, which is meaningless : the only purpose of the filter is to modify the repartition of colors on the [0,1] interval in order to improve the readibility of the graph.
For instance, I get :
I'm happy with the left part, but not the right one, where the colorbar should be something like:
Here the filter function is clearly not the best one, but it should provide to you a better illustration.
I tried to redefine values
just before the definition of the colorbar :
# Definition of the colorbar :-(
new_val_map = {(i,j): filtR(G[i][j]['label']) for (i,j) in G.edges()}
new_values = [scalarMap.to_rgba(val_map[e]) for e in G.edges()]
sm = cmx.ScalarMappable(cmap=cmx.coolwarm)
sm.set_array(new_values)
plt.colorbar(sm)
But nothing changes.
My understanding of Matplotlib is kind of limited, and the presented code is already a patchwork of stack overflow answers.