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

时间:2018-12-27 14:02:57

标签: python matplotlib networkx

我有一个名为“ 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坐标。

1 个答案:

答案 0 :(得分:0)

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

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绘图,您可以使用 mplot3d here是一个示例。