networkx中的边缘标签不在边缘上

时间:2019-11-14 18:33:35

标签: python pandas matplotlib networkx

首先,我要说的是,我们现在正在上一门高中的python课,对此我们来说是非常陌生的。我们正在集体合作,由老师提供一个项目,该项目要求我们从repl.it上提供给我们的表格中绘制一张地图。我们使用networkx和matplotlib绘制了地图,但是每当我们尝试向边缘添加标签时,它都无法正确绘制。 (这是我们第一次使用这些模块)

编辑:在我发布代码之前,结果是我们的一位项目成员将代码弄乱了。现在,我们主要在行上使用标签,但是间距仍然不正确,这意味着其中大部分不可读。基本上,我们现在想知道如何正确设置图形的格式,以使其易于阅读且间距正确。我更新了以下代码

import matplotlib as mpl
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('Agg')
import networkx as nx
import pandas as pd
import replit as re

df = pd.read_csv('map.txt')

df = df.nsmallest(129,'startbuilding')
print(df)
startb = df.loc[:,'startbuilding']
endb = df.loc[:,'EndBuilding']
totd = df.loc[:,'TOTDIS']
outd = df.loc[:,'OUTDis']

print("Loading...")
re.clear()
G = nx.DiGraph()
plt.figure()

nodes = []



for i in range(len(startb)):
  if startb[i] not in nodes:
    nodes.append(startb[i]) 


print(nodes)


G.add_nodes_from(nodes)

for i in (range(len(startb))):
  G.add_edge(startb[i],endb[i],length=totd[i])


pos = nx.spring_layout(G, dim=2, k=None, fixed=None, iterations=50, weight='weight', scale=2.0)






nx.draw_networkx_edge_labels(G,pos = pos ,font_size = 3)


plt.axis('off')
nx.draw(G, with_labels = True,pos = pos)
plt.show()
plt.savefig('map.png')

间距很奇怪,图形也不可读: The spacing is weird and the graph is unreadable

1 个答案:

答案 0 :(得分:0)

事实证明,我们最终只是单独绘制每个节点并放弃了绘制算法,现在它运行良好。感谢所有回应建议的人

相关问题