求問大神:
用來做社會網絡分析中的直方圖的,代碼如下:
import networkx as net
import matplotlib.pyplot as plot
def sorted_map(map):
ms=sorted(map.items(),key=lambda k:(-k[1],k[0]))
return ms
g=net.read_pajek('russians.net')
deg=net.degree(g)
ds=sorted_map(deg)
print(ds[0:9])
h=plot.hist(deg.values(),100)
plot.loglog(h[1][1:],h[0])
結果運行之後顯示的是:
Traceback (most recent call last):
File "degreeCentrality.py", line 14, in <module>
h=plot.hist(deg.values())
File "C:\Users\19665\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\pyplot.py", line 2965, in his
t
stacked=stacked, data=data, **kwargs)
File "C:\Users\19665\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\__init__.py", line 1819, in i
nner
return func(ax, *args, **kwargs)
File "C:\Users\19665\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\axes\_axes.py", line 5935, in
hist
x = _normalize_input(x, 'x')
File "C:\Users\19665\AppData\Local\Programs\Python\Python35\lib\site-packages\matplotlib\axes\_axes.py", line 5877, in
_normalize_input
"{ename} must be 1D or 2D".format(ename=ename))
ValueError: x must be 1D or 2D
找了一下午了,都沒找到相關的解決方法,跪求指點!
只需要將倒數第二行的h=plot.hist(deg.values(),100)改為h=plot.hist(list(deg.values()),100)就可以了,python 3.x