【问题标题】:Error displaying Edge labels from pandas dataframe networkx/ error with pos values. dont know which is it显示来自 pandas 数据框 networkx/ 带有 pos 值的错误的边缘标签时出错。不知道是哪个
【发布时间】:2021-02-18 20:58:19
【问题描述】:

我的数据框有 3 列,源、目标和值。看起来像这样

源目标值 波巴费特 C-3PO 4 波巴费特 CHEWBACCA 3 波巴·费特达斯·维达 8 BOBA FETT HAN 7

G = nx.from_pandas_edgelist(links,source='source',target='target', edge_attr='value')

我用它来添加我的边缘列表

nx.draw_networkx_edge_labels(G,pos=nx.Graph(G),edge_labels={(u,v):w for u,v,w in G.edges(data='value')})


我试过这个来显示边缘标签。我希望将“价值”显示为我的边缘标签 它给出了这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-d49aadc4a5f6> in <module>
      1 ##try edge list here
----> 2 nx.draw_networkx_edge_labels(G,pos=nx.Graph(G),edge_labels={(u,v):w for u,v,w in G.edges(data='value')})
      3 

~\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py in draw_networkx_edge_labels(G, pos, edge_labels, label_pos, font_size, font_color, font_family, font_weight, alpha, bbox, ax, rotate, **kwds)
    939     text_items = {}
    940     for (n1, n2), label in labels.items():
--> 941         (x1, y1) = pos[n1]
    942         (x2, y2) = pos[n2]
    943         (x, y) = (x1 * label_pos + x2 * (1.0 - label_pos),

ValueError: too many values to unpack (expected 2)

【问题讨论】:

    标签: python pandas loops networkx


    【解决方案1】:

    您提供给pos 的值应该是节点位置字典而不是实际图形(networkx 中有一堆布局函数可以构建它。这样的东西应该可以解决它。

    gpos = nx.circular_layout(G) # make a variable containing not positions
    nx.draw_networkx(G,pos=gpos) # draw the graph
    nx.draw_networkx_edge_labels(G,pos=gpos,edge_labels={(u,v):w for u,v,w in G.edges(data='value')}) # add edge labels
    

    如果您多次调用来绘制图表的其余部分,最好创建一个包含节点位置的变量,并将相同的变量传递给上述函数的 pos 参数。

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      相关资源
      最近更新 更多