Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用networkX的python多向图_Python_Networkx - Fatal编程技术网

使用networkX的python多向图

使用networkX的python多向图,python,networkx,Python,Networkx,问题相对简单,我找不到任何相同的指针。我有一个具有以下值的csv: Source Destination value a b 0.7 b c 0.58 c d 0.4 a d 0.52 b d 0.66 d b 0.30 a c 0.33 我想用python进行图形分析,

问题相对简单,我找不到任何相同的指针。我有一个具有以下值的csv:

 Source Destination value
    a         b     0.7
    b         c     0.58
    c         d     0.4
    a         d     0.52
    b         d     0.66
    d         b     0.30
    a         c     0.33
我想用python进行图形分析,我发现networkx是合适的选择。我使用下面的代码实现了同样的功能

import networkx as nx
import pandas as pd

df = pd.read_csv('values.csv')

G = nx.from_pandas_edgelist(df, source='Source', target='Destination', edge_attr='value',)   

G.nodes()

G.edges()


nx.draw_networkx(G, with_labels=True)
由于csv中的值有b->d和d->b,这将是一个多向图,但当我尝试输出结果时,我没有得到这些值

G.nodes()
NodeView(('a', 'b', 'c', 'd'))

G.edges()
EdgeView([('a', 'b'), ('a', 'd'), ('a', 'c'), ('b', 'c'), ('b', 'd'), ('c', 'd')])

我想了解为什么我不能在边缘响应中获得d->b的边缘

我在文档中找到了nx.MultiDiGraph,但我不确定如何使用它

import networkx as nx
import pandas as pd

df = pd.read_csv('values.csv')

G = nx.from_pandas_edgelist(df, source='Source', target='Destination', edge_attr='value',)   

G.nodes()

G.edges()


nx.draw_networkx(G, with_labels=True)

谢谢

在创建图形时,您应该只使用
有向图

G = nx.from_pandas_edgelist(df, source='Source', target='Destination', edge_attr='value', create_using=nx.DiGraph)
print(G.edges())
# OutEdgeView([('a', 'b'), ('a', 'd'), ('a', 'c'), ('b', 'c'), ('b', 'd'), ('c', 'd'), ('d', 'b')])

除了@KPLauritzen的答案外,我还使用以下方法获得了多向加权图:

Graphtype = nx.MultiDiGraph()

G = nx.from_pandas_edgelist(df, source='Source', target='Destination', edge_attr='value',create_using=Graphtype)   

这管用!非常感谢你。不过,我很快就会怀疑,如果我想在边上绘制带有权重标签的图表,是否可以这样做。我尝试了
nx.draw\u networkx(G,带标签=True)
但它不显示边上的权重我不知道是否可以标记边。有关可视化的可能示例,请参见