I have a plot of a networkx graph in which edge-color depends on the weights assigned to the respective edges using the following code (with a_netw
the nx.Graph):
a_netw_edges = a_netw.edges()
a_netw_weights = [a_netw[source][dest]['weight'] for source, dest in a_netw_edges]
a_netw_colors = [plt.cm.Blues(weight*15) for weight in a_netw_weights]
nx.draw_networkx(a_netw, edges=a_netw_edges, width=1, edge_color=a_netw_colors)
To this graph I would like to add a legend that makes the connection between the weights and the colours explicit; like in a heatmap that uses pcolor
.
While I have a rough idea of how to start:
fig, axes = plt.subplots(nrows=2)
nx.draw_networkx(a_netw, edges=a_netw_edges, width=1, edge_color=a_netw_colors, ax=axes[0])
axes[0].get_xaxis().set_visible(False)
axes[0].get_yaxis().set_visible(False)
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
axes[1].imshow(gradient, aspect=3, cmap=plt.cm.Blues)
axes[1].get_yaxis().set_visible(False)
plt.tight_layout()
I have no idea how to do the following steps:
- Add the correct ticks on the relevant axis to get the connection with the weights.
- Draw it vertically instead of horizontally.