I'm trying to make a graph in networkx. I'm having trouble assigning different node sizes to the nodes.
Here is my code I've been playing with:
import sys
from collections import defaultdict
import networkx as nx
import matplotlib.pyplot as plt
inp = sys.argv[1]
cluster = sys.argv[1] + ".cluster"
counts = sys.argv[1] + ".counts"
with open(cluster, "r") as f1:
edges = [line.strip().split('\t') for line in f1]
with open(counts, "r") as f2:
countsdic = defaultdict(list)
for line in f2:
k,v = line.strip().split()
countsdic[k].append(v)
tmp = []
for el in sum(edges, []):
tmp.append(el)
nodes = []
for t in tmp:
if t not in nodes:
nodes.append(t)
node_sizes = {}
for n in nodes:
node_sizes[n] = ' '.join(countsdic[n])
print node_sizes
nodes2 = []
sizes = []
for k in node_sizes.keys():
nodes2.append(k)
for v in node_sizes.values():
sizes.append(v)
print nodes2
print len(nodes2)
print sizes
print len(sizes)
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
nx.draw_random(g, node_list = nodes2, node_size = sizes)
# I've also tried assigning node_list and node_size with node_sizes.keys() and node_sizes.values()
plt.savefig(inp + "." + gtype + ".png")
plt.show()
If I do not attempt to change the node sizes, I get a pretty decent graph. The dictionary values are between 1 and 10, with a few high values like 156 which I need to be the largest, so I would need to do something like: node_sizes = [n*100 for n in sizes] for the smaller values to at least appear on the graph and the larger values to appear relevant, but that didn't work either.
The error I get is:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
self.show()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/collections.py", line 717, in draw
for x in self._sizes]
TypeError: Not implemented for this type
After a couple hours of google searching, I am unable to resolve the issue. Here is a generated without changing the node sizes:
All comments and help are appreciated.