【问题标题】:Problem with setting edges labels in a graph of nodes with given position coordinates在具有给定位置坐标的节点图中设置边标签的问题
【发布时间】:2018-12-27 14:02:56
【问题描述】:

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

【问题讨论】:

    标签: python matplotlib networkx


    【解决方案1】:

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

    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 是一个示例。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2019-06-25
      • 2023-04-01
      相关资源
      最近更新 更多