0

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: click here to see the image

link for csv file

this is the csv file i was using

  • 1
    Can you supply a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example)? This would help us to answer your question and narrow down the number of causes. – Sparky05 Jan 20 '22 at 11:57
  • I have edited the code to the bare minimum and removed the unnecessary stuff.Basically if you see the labels above edges are flying and i want to position them on the edges and align them.Thanks in advance for the help – Holkar Klein Jan 20 '22 at 12:04
  • The problem is that we don't have the csv/data and probably stuff like the pandas data frame has no effect on the plotting issue. – Sparky05 Jan 20 '22 at 13:04
  • Please don't post images of code/data/error messages. Post the text directly here on SO and specify whether this is a problem of the displayed figure, the stored figure, or both. Also, I suggest reading [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Mr. T Jan 20 '22 at 13:22
  • i have added my code my result figure where my edge labels are not placed properly on the edges (its the problem) and has also added link to csv file.Can you please help me in positioning or aligning the edge labels correctly on edges – Holkar Klein Jan 20 '22 at 13:34
  • The spacing between edges is still very less and every time it is making random shape graph.I would be highly thankful to you if you can suggest me how to increase spacing between edges and make my graph more readable – Holkar Klein Jan 20 '22 at 17:19

1 Answers1

0

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'))
warped
  • 8,947
  • 3
  • 22
  • 49
  • It has helped me in positioning the labels correctly on the edges but the spacing between edges is still very less and every time it is making random shape graph.I would be highly thankful to you if you can suggest me how to increase spacing between edges and make my graph more readable – Holkar Klein Jan 20 '22 at 13:59