2016-08-11 13 views
0

私は、NetworkXを使用してPython 2.7で動作する以下のコードを持っています。NetworkX Degreeのヒストグラム - Python 2とPython 3

plt.hist(nx.degree(G).values()) 
plt.xlabel('Degree') 
plt.ylabel('Number of Subjects') 
plt.savefig('network_degree.png') #Save as file, format specified in argument 
私は、Python 3の下で、この同じコードを実行してみてください

enter image description here

、私は次のエラーを取得する:

Traceback (most recent call last): 
    File "filename.py", line 71, in <module> 
    plt.hist(nx.degree(G).values()) 
    File "/Users/user/anaconda/envs/py3/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2958, in hist 
    stacked=stacked, data=data, **kwargs) 
    File "/Users/user/anaconda/envs/py3/lib/python3.5/site-packages/matplotlib/__init__.py", line 1812, in inner 
    return func(ax, *args, **kwargs) 
    File "/Users/user/anaconda/envs/py3/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5960, in hist 
    x = _normalize_input(x, 'x') 
    File "/Users/user/anaconda/envs/py3/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5902, in _normalize_input 
    "{ename} must be 1D or 2D".format(ename=ename)) 
ValueError: x must be 1D or 2D 

I基本的に、それはちょうどそうのような程度ノードのヒストグラムをプロット私はかなり単純なコードになると期待していたものを使って、今はPython 3で混乱し始めています。何が変わったの?

+0

のサポートだけでHTTPSをマスターする合併しました: //github.com/matplotlib/matplotlib/pull/6787 – tacaswell

答えて

3

Python2では、dict.valuesメソッドがリストを返します。 plt.histリストを受け付けますので

In [197]: nx.degree(G).values() 
Out[197]: dict_values([2, 2, 2, 2]) 

ではなくdict_valuesオブジェクト、dict_valuesは、リストに変換します:のpython3で 、それはa dict_values objectを返す辞書ビューの

plt.hist(list(nx.degree(G).values())) 
関連する問題