Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 错误的networkx图形和可视化是丑陋的_Python_Networkx_Graph Theory - Fatal编程技术网

Python 错误的networkx图形和可视化是丑陋的

Python 错误的networkx图形和可视化是丑陋的,python,networkx,graph-theory,Python,Networkx,Graph Theory,我有一个dataframe,它由60多个(2x2)大小的值组成,下面是一些数据的示例 label data_pairs -1040 [(-1037.13, -1044.77)] -1092 [(-1102.64, -1081.64)] -1105 [(-1107.36, -1102.64)] -137 [(-136.19, -138.75)] -1431 [(

我有一个dataframe,它由60多个(2x2)大小的值组成,下面是一些数据的示例

   label           data_pairs
   -1040         [(-1037.13, -1044.77)]
   -1092         [(-1102.64, -1081.64)]
   -1105         [(-1107.36, -1102.64)]
   -137          [(-136.19, -138.75)]
   -1431         [(-1434.08, -1429.31)]
   -612          [(-622.47, -601.98)]
   -672          [(-669.77, -674.95)]
   -752          [(-748.22, -755.9)]
   -791          [(-795.19, -788.02)]
df=pd.read_excel (r'C:\Users\xxx\Documents\Python Scripts\Python 
                 Scripts\uuu\data_pairs.xlsx')   # type: datFrame (10,2)
import networkx as nx

G = nx.DiGraph()
edges = np.stack(df.Pairs.to_numpy()).reshape(-1,2)   # array of str1248 (5,2)
G = nx.from_edgelist(edges)
pos = nx.spring_layout(G, k=0.6)     # dict

fig=plt.figure(figsize=(10,2),dpi=300)
nx.draw(G, pos=pos, node_size=800, with_labels=True,node_color='y')
正如数据集中的名称所示,我想为
对的值绘制
networkx
图,并相应地标记它们。根据StackOverflow用户的一个建议,我根据他的指导进一步修改了代码,如下所示

   label           data_pairs
   -1040         [(-1037.13, -1044.77)]
   -1092         [(-1102.64, -1081.64)]
   -1105         [(-1107.36, -1102.64)]
   -137          [(-136.19, -138.75)]
   -1431         [(-1434.08, -1429.31)]
   -612          [(-622.47, -601.98)]
   -672          [(-669.77, -674.95)]
   -752          [(-748.22, -755.9)]
   -791          [(-795.19, -788.02)]
df=pd.read_excel (r'C:\Users\xxx\Documents\Python Scripts\Python 
                 Scripts\uuu\data_pairs.xlsx')   # type: datFrame (10,2)
import networkx as nx

G = nx.DiGraph()
edges = np.stack(df.Pairs.to_numpy()).reshape(-1,2)   # array of str1248 (5,2)
G = nx.from_edgelist(edges)
pos = nx.spring_layout(G, k=0.6)     # dict

fig=plt.figure(figsize=(10,2),dpi=300)
nx.draw(G, pos=pos, node_size=800, with_labels=True,node_color='y')
networkx
代码如下

   label           data_pairs
   -1040         [(-1037.13, -1044.77)]
   -1092         [(-1102.64, -1081.64)]
   -1105         [(-1107.36, -1102.64)]
   -137          [(-136.19, -138.75)]
   -1431         [(-1434.08, -1429.31)]
   -612          [(-622.47, -601.98)]
   -672          [(-669.77, -674.95)]
   -752          [(-748.22, -755.9)]
   -791          [(-795.19, -788.02)]
df=pd.read_excel (r'C:\Users\xxx\Documents\Python Scripts\Python 
                 Scripts\uuu\data_pairs.xlsx')   # type: datFrame (10,2)
import networkx as nx

G = nx.DiGraph()
edges = np.stack(df.Pairs.to_numpy()).reshape(-1,2)   # array of str1248 (5,2)
G = nx.from_edgelist(edges)
pos = nx.spring_layout(G, k=0.6)     # dict

fig=plt.figure(figsize=(10,2),dpi=300)
nx.draw(G, pos=pos, node_size=800, with_labels=True,node_color='y')
值如下所示

[(-1037.13, -1044.77)]  [(-1102.64, -1081.64)]
[(-1107.36, -1102.64)]  [(-1187.16, -1192.42)]
[(-1261.33, -1280.02)]  [(-136.19, -131.06)]
[(-131.06, -138.75)]    [(-136.19, -138.75)]
[(-1434.08, -1429.31)]  [(-304.94, -308.8), (-304.94, -307.85)]

 
这个
networkx
graph我得到了这样的东西

正如您所看到的,这些图看起来是错误的,因为它们使用了两个值来连接图形的
值。此外,图中未显示标签。例如,对于
数据对
值[(-1037.13,-1044.77)],我应该在行之间获得
标签
值-1040。类似于这样的东西
(-1037.13)------------------------->(-1044.77)
和类似的东西。
-1040

以上一点只是为了理解


你能帮我怎么得到这样的结果吗?提前多谢了

问题是您的列包含嵌套在列表中的元组。您需要正确构造这些值,以便
networkX
按预期构建rgaph。一种简单的方法是将值堆叠到一个数组中,并适当地重塑形状

另外,
nx.spring\u layout
有一个参数
k
,用于设置图形中节点之间的间距:

from ast import literal_eval
from itertools import chain

df['data_pairs']= df['data_pairs'].map(literal_eval)
G = nx.from_edgelist(chain.from_iterable(df.data_pairs))

plt.figure(figsize=(10,5))
pos = nx.spring_layout(G, k=.6)
nx.draw(G, 
        pos=pos,
        node_size=800, 
        with_labels=True, 
        node_color='y')

输入数据-

G.edges()
EdgeView([(-1037.13, -1044.77), (-1102.64, -1081.64), (-1102.64, -1107.36), 
          (-136.19, -138.75), (-1434.08, -1429.31), (-622.47, -601.98), 
          (-669.77, -674.95), (-748.22, -755.9), (-795.19, -788.02), 
          (-304.94, -308.8), (-304.94, -307.85)])

使用这一行并尝试定义边后,出现错误“所有输入数组必须具有相同的形状”。边缘线代码正确吗?请不要使用标签,除非您的问题涉及IGRAPHE库。非常感谢。最后一个问题,如何在节点之间显示“键”?最后:)不用担心。你说钥匙是什么意思@拉维面临着可视化问题,我把它作为一个新问题发布。你能帮我看看吗?