【问题标题】:plotting integer output from Networkx绘制 Networkx 的整数输出
【发布时间】:2016-03-21 08:07:00
【问题描述】:

我从集合、networkx(作为 nx)和 matplotlib(作为 plt)导入了计数器。我已经在我制作的图表中获取了每个节点的度数。但后来我想绘制度数与度数的关系。 我的尝试如下:

deg=list(G.degree().values())# set the degree for each node
for i in deg:# iterate this list
    count=Counter(i)#count the frequency of the nodes
    plt.plot(count, i)#plot frequency vs degree

但是我不断收到错误:

TypeError: 'int' object is not iterable

所以我想知道我的输入是否正确,以及我的逻辑一开始是否正确,因为我不确定这些频率甚至会链接回它们的派生度并正确绘制它们。

【问题讨论】:

  • 显示完整的回溯。它会显示错误发生在哪一行。

标签: python matplotlib networkx


【解决方案1】:

您在 list 或 int 上迭代 i,这很好,但接下来是这一行

count=Counter(i)#count the frequency of the nodes

没有意义,你在数i,它是一个整数。你可能想做的(假设这是collections.Counter),是

count = Counter(deg)

这将计算deg 中的所有出现次数。

【讨论】:

    【解决方案2】:

    您可能想尝试networkx.degree_histogram,它会计算您想要的数据。它对图 G 的作用是

    degseq = list(d for n, d in G.degree())
    dmax = max(degseq) + 1
    freq = [ 0 for d in range(dmax) ]
    for d in degseq:
        freq[d] += 1
    

    【讨论】:

    • 非常感谢,但我无法使用 plt.show() 显示输出...有什么我遗漏的吗?
    • 是的,绘制频率数组,比如说 import matplotlib.pyplot as plt, plt.hist(freq), plt.show()。
    猜你喜欢
    • 2015-04-06
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2021-07-27
    • 2013-10-13
    • 2011-04-28
    • 2017-07-06
    相关资源
    最近更新 更多