问题描述
我的数据框有 3 列,源、目标和值.看起来像这样
My dataframe has 3 columns, source , target and value. it looks like this
源目标值波巴费特 C-3PO 4波巴费特 CHEWBACCA 3波巴·费特达斯·维达 8波巴费特汉 7
source target valueBOBA FETT C-3PO 4BOBA FETT CHEWBACCA 3BOBA FETT DARTH VADER 8BOBA FETT HAN 7
G = nx.from_pandas_edgelist(links,source='source',target='target', edge_attr='value')
我用它来添加我的边缘列表
I use this to add my edgelist
nx.draw_networkx_edge_labels(G,pos=nx.Graph(G),edge_labels={(u,v):w for u,v,w inG.edges(data='value')})
我试过这个来显示边缘标签.我希望将值"显示为我的边缘标签它给出了这个错误:
I tried this to show edge labels. I want the 'value' to be dispplayed as my edge labelit gives this error:
---------------------------------------------------------------------------
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)
推荐答案
你提供给 pos
的值应该是节点位置的字典,而不是实际的图形(里面有一堆布局函数networkx 来构建它.像这样的东西应该可以修复它.
The value you provide to pos
should be a dictionary of node positions not the actual graph (there are a bunch of layout functions in networkx to build this. Something like this should fix it.
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 参数.
If you make multiple calls to plot the rest of the graph it's best to make a variable that contains your node positions and pass that same variable to the pos argument of the functions as above.
这篇关于使用 pos 值显示来自 pandas 数据帧 networkx/错误的边缘标签时出错.不知道是哪个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!