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]
解决方案
原来的:
pos[index] = (path[index][0], path[index][1])
事实证明,我需要将值转换为浮点数。
固定的:
pos[index] = (float(path[index][0]), float(path[index][1]))
推荐阅读
- angular - 如果后端是 Windows 服务 (Windows Server 2016),我在哪里托管 Angular 脚本?
- python - 如何删除 f 字符串表达式中的负号?
- ssl - 让我们加密 SSL 证书也可以用于 websocket 连接吗?
- c# - 所有者绘制的列表视图无法正确自动调整列大小
- babeljs - @babel/plugin-proposal-class-properties vs babel-plugin-transform-class-properties
- sql-server - Sum XML Value based on different attribute types
- r - regex expression to match decimal numbers with comma as a separator
- azure-logic-apps - 连接字符串更改是否需要在逻辑应用中进行新部署?
- heroku - How to set environment variable on heroku on just workers?
- excel - Excel VBA Math just not adding up