gpt4 book ai didi

python - 在具有给定位置坐标的节点图中设置边标签的问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:34 24 4
gpt4 key购买 nike

我有一个名为“loc”的 11x11 数据框,其中包含节点的位置坐标(X、Y 和 Z),并以节点名称作为索引。我有另一个数据框“dist”,其中包含节点之间的距离,而列标题和索引上都包含节点名称。我想绘制网络图,以便每个节点都标有其名称(loc 的索引)。此外,每个节点都应该连接到所有其他节点。

但是我希望每条边都标有两个节点之间的距离。我

我已经创建了图表,用标签绘制了节点并在它们之间绘制了边。但是无法为图例绘制标签。

#####  Example data similar to my actual data
coord = np.random.randint(low=1, high=100, size=(11,3))
lab = ['AP', 'STA1', 'STA2', 'STA3', 'STA4', 'STA5', 'STA6', 'STA7',
'STA8', 'STA9', 'STA10']
loc = pd.DataFrame.from_records(coord, columns=['X', 'Y', 'Z'],
index=lab)
d = distance_matrix(loc,loc) # Distance between each device
dist = pd.DataFrame.from_records(d, columns=lab)
dist = dist.set_index(dist.columns)
dist = dist.round(decimals=1)

########## Network Plot without edges
coord = np.random.randint(low=1, high=100, size=(11,3))
lab = ['AP', 'STA1', 'STA2', 'STA3', 'STA4', 'STA5', 'STA6',
'STA7', 'STA8', 'STA9', 'STA10']
loc = pd.DataFrame.from_records(coord, columns=['X', 'Y', 'Z'],
index=lab)


d = distance_matrix(loc,loc) # Distance between each device
dist = pd.DataFrame.from_records(d, columns=lab)
dist = dist.set_index(dist.columns)
dist = dist.round(decimals=1)

# Add the nodes to graph
G=nx.Graph()
for i in range(len(loc)):
G.add_node(loc.index[i])

pos = loc.ix[:,0:2].transpose().to_dict(orient='list')

# Add edges
for i in range(len(loc)):
for j in range(len(loc)):
G.add_edge(loc.index[i], loc.index[j])

# Draw the network
fig, ax = plt.subplots(figsize=(5,5))
ax.axis('equal')
nx.draw_networkx(G, pos=pos, arrows= True, with_labels=True, node_size=600,
node_shape='o', alpha=0.5, font_size=10)
plt.show()

1) 给定的代码生成所需的图形,但没有边标签。我想在边缘的中心或其他地方绘制标签以使其更具可读性。请记住,edge_label 表示两个节点之间的距离(即索引和列标题具有相同值的“dist”数据帧中的值)。2)我们可以在 3D 中绘制相同的网络,因为节点具有三个坐标(X、Y 和 Z)。正如在我的代码中一样,我只绘制 X 和 Y 坐标。

最佳答案

要绘制边缘的标签,您需要首先将此数据添加到边缘:

G.add_edge(loc.index[i], loc.index[j], weight=d[i][j])

然后为了绘制标签,您可以调用nx.draw_networkx_edge_labels()

一起:

import numpy as np
import pandas as pd
import networkx as nx
import scipy
import matplotlib.pyplot as plt

##### Example data similar to my actual data
coord = np.random.randint(low=1, high=100, size=(11,3))
lab = ['AP', 'STA1', 'STA2', 'STA3', 'STA4', 'STA5', 'STA6', 'STA7',
'STA8', 'STA9', 'STA10']
loc = pd.DataFrame.from_records(coord, columns=['X', 'Y', 'Z'],
index=lab)
d = scipy.spatial.distance_matrix(loc,loc) # Distance between each device
dist = pd.DataFrame.from_records(d, columns=lab)
dist = dist.set_index(dist.columns)
dist = dist.round(decimals=1)

########## Network Plot without edges
coord = np.random.randint(low=1, high=100, size=(11,3))
lab = ['AP', 'STA1', 'STA2', 'STA3', 'STA4', 'STA5', 'STA6',
'STA7', 'STA8', 'STA9', 'STA10']
loc = pd.DataFrame.from_records(coord, columns=['X', 'Y', 'Z'],
index=lab)


d = scipy.spatial.distance_matrix(loc,loc) # Distance between each device
dist = pd.DataFrame.from_records(d, columns=lab)
dist = dist.set_index(dist.columns)
dist = dist.round(decimals=1)

# Add the nodes to graph
G=nx.Graph()
for i in range(len(loc)):
G.add_node(loc.index[i])

pos = loc.ix[:,0:2].transpose().to_dict(orient='list')

# Add edges
for i in range(len(loc)):
for j in range(len(loc)):
G.add_edge(loc.index[i], loc.index[j], weight='%.2f' % d[i][j])

# Draw the network
fig, ax = plt.subplots(figsize=(5,5))
ax.axis('equal')
nx.draw_networkx(G, pos=pos, arrows= True, with_labels=True, node_size=600, node_shape='o', alpha=0.5, font_size=10)
edge_labels = {(u, v): d['weight'] for u, v, d in G.edges(data=True)}
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8, rotate=False)
plt.show()

对于 3D 绘图,您可以使用mplot3dhere就是一个例子。

关于python - 在具有给定位置坐标的节点图中设置边标签的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com