程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> python-Python中matplotlib.pyplot.hist顯示x must be 1D or 2D

python-Python中matplotlib.pyplot.hist顯示x must be 1D or 2D

編輯:編程解疑
Python中matplotlib.pyplot.hist顯示x must be 1D or 2D

求問大神:
用來做社會網絡分析中的直方圖的,代碼如下:

 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved