簡體   English   中英

在具有給定位置坐標的節點圖中設置邊緣標簽的問題

[英]Problem with setting edges labels in a graph of nodes with given position coordinates

我有一個名為“ 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繪圖,您可以使用mplot3d是一個示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM