8

I was wondering how can I simply add a colorbar to my plot. I have to following code, which plots a graph by reading a gml file. I have a set of numbers to assign to the edges as their colors and I just want to see a colorbar right next to the plot so I can analyze the colors. When I add the plt.colorbar(g) it gives me error. How can I add a colorbar without going through all the process of actually building the colorbar?

H = nx.read_gml('./network1.gml')

EAM = EigenVectorCentrality( EAMatrix );

x = [];
for eam in EAM[0]:
    x.append(eam[0]);

 degs =  H.degree().values();
 plt.clf()
 g = nx.draw(H, with_labels=0, edge_color=x, node_size=70, font_size=9, width=1)
 plt.axis('equal')     
 plt.colorbar(g);
 plt.show()

And here is the Nerwork1.gml file:

graph
[
   node
   [
    id 1
   ]
 node
[
    id 2
]
node
[
    id 3
]
node
[
    id 4
]
    node
[
    id 5
]
    node
[
    id 6
]
    node
[
    id 7
]
    node
[
    id 8
]
    node
[
    id 9
]
    node
[
    id 10
]
    node
[
    id 11
]
edge
[
    source 1
    target 2
]
edge
[
    source 1
    target 2
]
edge
[
    source 1
    target 3
]
edge
[
    source 1
    target 4
]
edge
[
    source 1
    target 5
]
edge
[
    source 2
    target 3
]
edge
[
    source 2
    target 4
]
edge
[
    source 2
    target 5
]
edge
[
    source 3
    target 4
]
edge
[
    source 3
    target 5
]
edge
[
    source 4
    target 5
]
edge
[
    source 6
    target 7
]
edge
[
    source 6
    target 8
]
edge
[
    source 6
    target 9
]
edge
[
    source 6
    target 10
]
edge
[
    source 7
    target 8
]
edge
[
    source 7
    target 9
]

edge
[
    source 7
    target 10
]
edge
[
    source 8
    target 9
]
edge
[
    source 8
    target 10
]
edge
[
    source 9
    target 10
]
edge
[
    source 5
    target 6
]
edge
[
    source 5
    target 11
]
 edge
 [
    source 6
    target 11
 ]
]
Mr.Boy
  • 615
  • 1
  • 7
  • 13

2 Answers2

14

Since I did not have your data available, I used this simple example from the networx homepage. But it should be trivial for you to use it in your code.

import matplotlib.pyplot as plt
import networkx as nx

G=nx.star_graph(20)
pos=nx.spring_layout(G)
colors=range(20)
cmap=plt.cm.Blues
vmin = min(colors)
vmax = max(colors)
nx.draw(G, pos, node_color='#A0CBE2', edge_color=colors, width=4, edge_cmap=cmap,
           with_labels=False, vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin = vmin, vmax=vmax))
sm._A = []
plt.colorbar(sm)
plt.show()

This does the trick, but I agree, it is a bit sad that nx.draw just returns None.

jgyou
  • 473
  • 1
  • 8
  • 19
hitzg
  • 12,133
  • 52
  • 54
  • 2
    Thanks, it is working, but it is sad that there is not an easy way to do that and all is because of `nx.draw` which returns `None` as you said. – Mr.Boy Nov 04 '14 at 17:46
  • It solved my problem but technically it didn't suggest any _simple_ way. Anyways thanks and I used your answer. Thanks – Mr.Boy Nov 04 '14 at 17:57
  • The suggested link is now broken. – FaCoffee May 09 '17 at 08:38
  • 2
    Thanks! Here's the new location: https://networkx.github.io/documentation/networkx-1.9/examples/drawing/edge_colormap.html – hitzg May 09 '17 at 15:19
  • 3
    I keep getting this error: `AttributeError: 'module' object has no attribute 'normalize'` referring to `norm=plt.normalize(vmin=vmin, vmax=vmax)`. I couldn't find it anywhere... – FaCoffee May 10 '17 at 14:19
  • I solved my problem. All I had to do was to call `mynodes=nx.draw_networkx_nodes()` and then call `plt.colorbar(nodes,cmap=whatever)` to make it work! – FaCoffee May 10 '17 at 14:48
  • 3
    works great, though I had to capitalize the "n" in "normalize". – eretmochelys Oct 11 '17 at 13:42
  • 3
    For those wondering why the comments don't make much sense anymore: The post has been edited. There's a capital in "Normalize" now. – jgyou Mar 08 '19 at 05:51
0

Old question, but for people looking for that now, you can also use nx.draw_networkx_nodes and nx.draw_networkx_edges separately, since both these functions return a pathcollection with can be given to plt.colorbar. For example

pos = nx.spring_layout(G)
pathcollection = nx.draw_networkx_nodes(G, pos, node_color=nx.get_node_attribute(G, "color"))
nx.draw_networkx_edges(G, pos)
plt.colorbar(pathcollection)
tbrugere
  • 755
  • 7
  • 17