我正在尝试在 networkx 中制作图表。我无法为节点分配不同的节点大小。
这是我一直在玩的代码:
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()
如果我不尝试更改节点大小,我会得到一个相当不错的图表。字典值介于 1 和 10 之间,有一些高值,如 156,我需要将其设置为最大,因此我需要执行以下操作:node_sizes = [n*100 for n in sizes] 对于较小的值至 at最少出现在图表上,较大的值显得相关,但这也不起作用。
我得到的错误是:
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
经过几个小时的谷歌搜索,我无法解决这个问题。这是在不更改节点大小的情况下生成的:
感谢所有评论和帮助。
最佳答案
2014/07/08 12:29PM:已更新以反射(reflect)@user3358205 的评论
问题是 drawing functions in NetworkX要求 node_sizes
作为 int
的 list
输入,而您传递的是 list
字符串。你可以read the parameters to the drawing functions here .
因为我没有您程序的输入文件,所以无法重现您的输出。但是,在下面的示例中,您通过传递 node_sizes
的 list
来改变节点的大小。请注意,在输出中,我按每个节点的大小标记了它们。
import sys, networkx as nx, matplotlib.pyplot as plt
# Create a list of 10 nodes numbered [0, 9]
nodes = range(10)
node_sizes = []
labels = {}
for n in nodes:
node_sizes.append( 100 * n )
labels[n] = 100 * n
# Node sizes: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900]
# Connect each node to its successor
edges = [ (i, i+1) for i in range(len(nodes)-1) ]
# Create the graph and draw it with the node labels
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
nx.draw_random(g, node_size = node_sizes, labels=labels, with_labels=True)
plt.show()
关于python - networkx - 根据列表或字典值更改节点大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24636015/