简体   繁体   中英

How to properly position edge labels on the edges of graph using networkx?

I have written a code to create a graph from csv file as below

import matplotlib.pyplot as plt
import networkx as nx
import pandas as pd
df = pd.read_csv("pracmap.csv")
g = nx.Graph()
Vlandict = {}
for idx, e in df.iterrows():
    edge_list = []
    EL={}
    ctr = 1
    for dev in ['DEVICE1', 'DEVICE3', 'DEVICE4', 'DEVICE2']:
        # print(e[dev])
        if e[dev] == "NONE":
            continue
        edge_list.append(e[dev])
        # print(edge_list)

        if ctr == 2:
            unode = edge_list[0]
            vnode = edge_list[1]
            Vlandict[(unode, vnode)] = e['VLAN']
            edge_list.reverse()
            edge_list.pop()

        else:
            ctr = ctr +1
print(g.nodes)
pos = nx.spring_layout(g, k=0.5, iterations=20)
nx.draw_networkx_edge_labels(g, pos, edge_labels=Vlandict, font_color='green',label_pos=0.5, rotate=False, font_size=8)
nx.draw(g, with_labels=1,node_shape="s",bbox=dict(facecolor="orange", edgecolor='black', boxstyle='round,pad=0.2'))
plt.savefig("pracmappp.png", format="PNG")
plt.show()


This kind of image i am getting where my edge labels are not positioned properly. I want to position these edges labels on edges:点击这里查看图片

link for csv file

this is the csv file i was using

instead of

nx.draw(g, with_labels=1,node_shape="s",bbox=dict(facecolor="orange", edgecolor='black', boxstyle='round,pad=0.2'))

use

nx.draw(g, pos=pos, with_labels=1,node_shape="s",bbox=dict(facecolor="orange", edgecolor='black', boxstyle='round,pad=0.2'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM