我正在尝试根据图形度值制作直方图。但是当我运行我的代码时,我收到了这个错误:
x只有一个数据点。必须给出垃圾箱或范围kwarg。
如果有人能告诉我我做错了什么,我会很感激。
代码是:
import numpy as np
import pandas
import networkx as nx
import unicodecsv as csv
import operator
import matplotlib.pyplot as plt
import scipy.stats as st
import community
import plotly.plotly as py
#graph
path="hero-network.csv"
graph = nx.Graph(name="Heroic Social Network")
with open(path, 'rb') as data:
reader = csv.reader(data)
for row in reader:
graph.add_edge(*row)
#histogram
plt.hist(graph.degree().values() , bins=500)
plt.title("Connectedness of Marvel Characters")
plt.xlabel("Degree")
plt.ylabel("Frequency")
plt.show()
csv datafile看起来像这样(前10个节点):
LITTLE, ABNER,"PRINCESS ZANDA"
LITTLE, ABNER,"BLACK PANTHER/T'CHAL"
BLACK PANTHER/T'CHAL,"PRINCESS ZANDA"
LITTLE, ABNER,"PRINCESS ZANDA"
LITTLE, ABNER,"BLACK PANTHER/T'CHAL"
BLACK PANTHER/T'CHAL,"PRINCESS ZANDA"
STEELE, SIMON/WOLFGA,"FORTUNE, DOMINIC"
STEELE, SIMON/WOLFGA,"ERWIN, CLYTEMNESTRA"
STEELE, SIMON/WOLFGA,"IRON MAN/TONY STARK "
STEELE, SIMON/WOLFGA,"IRON MAN IV/JAMES R."
答案 0 :(得分:1)
Matplotlib将iterables解释为单个值。在python 3中,dict.values
返回一个可迭代的。将其转换为列表,它将起作用:
plt.hist(list(graph.degree().values()), bins=500)