Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
使用python从间接图到有向图_Python_Python 3.x_Graph_Networkx - Fatal编程技术网

使用python从间接图到有向图

使用python从间接图到有向图,python,python-3.x,graph,networkx,Python,Python 3.x,Graph,Networkx,我想使用python将间接图转换为有向图。这是我的代码。我应该更改什么以获得有向图 我正在从graph.txt加载以下邻接矩阵(没有表示列标题或行名称的字母): 当前代码: import pandas as pd import numpy as np import networkx as nx # version 2.2 import matplotlib.pyplot as plt import re f = np.loadtxt ('graph.txt') ad=[] for x in

我想使用python将间接图转换为有向图。这是我的代码。我应该更改什么以获得有向图

我正在从graph.txt加载以下邻接矩阵(没有表示列标题或行名称的字母):

当前代码:

import pandas as pd
import numpy as np
import networkx as nx  # version 2.2
import matplotlib.pyplot as plt
import re

f = np.loadtxt ('graph.txt')
ad=[]
for x in f:
    ad.append(x)
print(ad)
df = pd.DataFrame(ad, columns = ['A','B','C','D','E'], index = ['A','B','C','D','E'])
G = nx.from_pandas_adjacency(df)
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, bbox = dict(fc="lightgreen", ec="black", boxstyle="circle", lw=3),
    width=2, arrowsize=30)
nx.draw_networkx_edge_labels(G, pos, edge_labels = nx.get_edge_attributes(G, 'weight'))
plt.show()


如何确定每条边的方向?根据关联矩阵:`A,B,C,D,E A1,1,0,1,0 B 0,-1,0,1,-1 C-1,0,-1,1,0 D 0,1,0,0,0 E-1,1,1,1,-1`,边必须从直线指向直线column@farahlana请在您的问题中添加更多信息。
graph.txt
的内容是什么?
import pandas as pd
import numpy as np
import networkx as nx  # version 2.2
import matplotlib.pyplot as plt
import re

f = np.loadtxt ('graph.txt')
ad=[]
for x in f:
    ad.append(x)
print(ad)
df = pd.DataFrame(ad, columns = ['A','B','C','D','E'], index = ['A','B','C','D','E'])
G = nx.from_pandas_adjacency(df)
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, bbox = dict(fc="lightgreen", ec="black", boxstyle="circle", lw=3),
    width=2, arrowsize=30)
nx.draw_networkx_edge_labels(G, pos, edge_labels = nx.get_edge_attributes(G, 'weight'))
plt.show()