【问题标题】:Problem while constructing networkx graph构建networkx图时的问题
【发布时间】:2020-10-26 09:24:12
【问题描述】:

我有以下 data 的数据框格式

  a       b
-510    -350
-300    -120
-350    -170
-170     30
 30      210
-120     30
-690    -510

使用这些数据,我正在尝试使用 python 构建networkx 图形,到目前为止编写了以下代码

import networkx as nx

G1 = nx.DiGraph()
G1.add_edges_from(data)
data_1= nx.spring_layout(G1)

fig1=plt.figure(figsize=(5,2),dpi=300)
nx.draw(G1, data_1, node_size=1000, with_labels=True, node_color='y',vertex_label_size=100)

但出现以下错误Edge tuple start must be a 2-tuple or 3-tuple,这基本上是由于添加边。我尝试了一些谷歌搜索找不到任何合适的答案来解决这个问题。你能帮我解决这个问题吗?提前致谢。

【问题讨论】:

标签: python networkx graph-theory


【解决方案1】:

要从 pandas 数据框构建图表,您需要 nx.from_pandas_edgelist

G1 = nx.from_pandas_edgelist(df, source='a', target='b', create_using=nx.DiGraph)
pos= nx.spring_layout(G1)

fig1=plt.figure(figsize=(5,2),dpi=300)
nx.draw(G1, pos, node_size=100, with_labels=True, node_color='y')

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多