4

I have points as x,y and I want to plot my graph using the (x,y) coordinates of my points list so that I can see the axis. here are my code and photo of the graph

import networkx as nx
import matplotlib.pyplot as plt
def add_edge_to_graph(G,e1,e2,w):
   G.add_edge(e1,e2,weight=w) 

G=nx.Graph()
points=[(1, 10), (8, 10), (10, 8), (7, 4), (3, 1)] #(x,y) points
edges=[(0, 1, 10), (1, 2, 5), (2, 3, 25), (0, 3, 3), (3, 4, 8)]#(v1,v2, weight)

for i in range(len(edges)):
       add_edge_to_graph(G,points[edges[i][0]],points[edges[i][1]],edges[i][2])
       
     
pos = nx.spring_layout(G)
nx.draw(G,pos=pos,node_color='k')
nx.draw(G, pos=pos, node_size=1500)  # draw nodes and edges
nx.draw_networkx_labels(G, pos=pos)  # draw node labels/names
# draw edge weights
labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
plt.axis()
plt.show() 

https://i.stack.imgur.com/criYx.png

Faisal
  • 43
  • 1
  • 4

3 Answers3

4

This should solve your problem:

import networkx as nx
import matplotlib.pyplot as plt


def add_edge_to_graph(G, e1, e2, w):
    G.add_edge(e1, e2, weight=w)


G = nx.Graph()
points = [(1, 10), (8, 10), (10, 8), (7, 4), (3, 1)]  # (x,y) points
edges = [(0, 1, 10), (1, 2, 5), (2, 3, 25), (0, 3, 3), (3, 4, 8)]  # (v1,v2, weight)

for i in range(len(edges)):
    add_edge_to_graph(G, points[edges[i][0]], points[edges[i][1]], edges[i][2])

# you want your own layout
# pos = nx.spring_layout(G)
pos = {point: point for point in points}

# add axis
fig, ax = plt.subplots()
nx.draw(G, pos=pos, node_color='k', ax=ax)
nx.draw(G, pos=pos, node_size=1500, ax=ax)  # draw nodes and edges
nx.draw_networkx_labels(G, pos=pos)  # draw node labels/names
# draw edge weights
labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, ax=ax)
plt.axis("on")
ax.set_xlim(0, 11)
ax.set_ylim(0,11)
ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)
plt.show()

Result

Figure with axes on and right positions

Backround

For the axis, I used plt.axis("on") as already suggested above together with How to make x and y axes appear when using networkx and matplotlib?

Additionaly, I replaced the spring_layout with the positions in your points list.

Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • @Faisal: Take a look [here](https://stackoverflow.com/help/someone-answers) and I would recommend to do the general tour. E.g. you could (have) edited your question to clarify your points and (after you collect some rep) you can also directly include figures in your question. – Sparky05 Nov 24 '20 at 15:26
0

I would try to turn axis on with :

limits = plt.axis("on")

So that you're able to use draw with axis.

Henry Mont
  • 337
  • 1
  • 3
  • 13
0

This doesn't use networkx, but quite handy if you quickly want to visualize points and see the edges.

sample code:

import matplotlib.pyplot as plt

# sample of edges where each node comprises of an x and y coordinate.
edges = [((84, 96), (93, 20)), ((93, 20), (94, 42)), ((94, 42), (98, 88))]

fig, ax = plt.subplots()
for edge in edges:
    x = [edge[0][0], edge[1][0]]
    y = [edge[0][1], edge[1][1]]
    ax.plot(x, y, '-o')

sample output:

enter image description here

Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51