为什么networkx不会绘制边缘?

时间:2019-05-06 00:20:48

标签: python networkx

我正在尝试使用networkx创建可视图形,但未显示边缘。节点在那里,但是边缘要么不显示,要么极其不正确。我看过其他帖子,但似乎都没有答案。

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx


def create_graph(filename, path):
    pos = {}
    G = nx.Graph()
    for index in range(0, len(path2)-1):
        pos[index] = (path[index][0], path[index][1])
    for index in range(0, len(path)-2):
        G.add_edge(index, index+1)
    print(G.edges())
    G.add_nodes_from(pos.keys())
    print(G.nodes())
    nx.draw(G, pos, node_color = 'b')

    name = filename.split('.')
    pic_name = name[0] + ".png"
    plt.savefig(pic_name)

我的原始程序不会产生任何边缘,但是简单的测试程序会产生边缘。

import graphing

list = [[32.4, 15.15],[12.5, 37.3],[236.3, 62.37],[235.3, 26.46],[324.27, 346.2],[25.45, 344.3],[34.23, 63.3]]

graphing.create_graph("test",list)

您可以批评我做错的所有其他事情,但是我主要关心的是为什么有时候边缘无法正常工作。我的方法明显有什么问题吗?

编辑: 在打印路径之后,吉布提数据的输出为:

[['22583.3333', '14300.0000'], ['21600.0000', '14150.0000'], ['21600.0000', '14966.6667'], ['21600.0000', '16500.0000'], ['20900.0000', '17066.6667'], ['20833.3333', '17100.0000'], ['23616.6667', '15866.6667'], ['23700.0000', '15933.3333'], ['23883.3333', '14533.3333'], ['24166.6667', '13250.0000'], ['25149.1667', '12365.8333'], ['26283.3333', '12766.6667'], ['26433.3333', '13433.3333'], ['26550.0000', '13850.0000'], ['27096.1111', '13415.8333'], ['27153.6111', '13203.3333'], ['27026.1111', '13051.9444'], ['27462.5000', '12992.2222'], ['27433.3333', '12400.0000'], ['27233.3333', '11783.3333'], ['26733.3333', '11683.3333'], ['26150.0000', '10550.0000'], ['27233.3333', '10450.0000'], ['27266.6667', '10383.3333'], ['27166.6667', '9833.3333'], ['26133.3333', '14500.0000'], ['22683.3333', '12716.6667'], ['22183.3333', '13133.3333'], ['21300.0000', '13016.6667'], ['22583.3333', '14300.0000']]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12), (12, 13), (13, 14), (14, 15), (15, 16), (16, 17), (17, 18), (18, 19), (19, 20), (20, 21), (21, 22), (22, 23), (23, 24), (24, 25), (25, 26), (26, 27), (27, 28)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]

1 个答案:

答案 0 :(得分:0)

原文:

pos[index] = (path[index][0], path[index][1])

原来,我需要将值强制转换为浮点数。

已修复:

pos[index] = (float(path[index][0]), float(path[index][1]))