कैसे (x, y) बिंदु सूची के निर्देशांक का उपयोग कर एक नेटवर्क ग्राफ को प्लॉट करें?

Nov 24 2020

मेरे पास x, y के रूप में अंक हैं और मैं अपनी बिंदु सूची के (x, y) निर्देशांक का उपयोग करके अपने ग्राफ़ को प्लॉट करना चाहता हूं ताकि मैं अक्ष देख सकूं। यहाँ मेरा कोड और ग्राफ का फोटो है

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.imgur.com/LbAGBIh.png

जवाब

1 Sparky05 Nov 24 2020 at 13:00

इससे आपकी समस्या का समाधान हो जाना चाहिए:

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()

परिणाम

backround

अक्ष के लिए, मैंने plt.axis("on")पहले से ही सुझाव दिया था कि नेटवर्कएक्स और मैटप्लोटलिब का उपयोग करते समय एक्स और वाई एक्सिस कैसे बनायें?

अतिरिक्त, मैंने spring_layoutआपकी pointsसूची में पदों के साथ बदल दिया ।

HenryMont Nov 24 2020 at 12:39

मैं अक्ष को चालू करने की कोशिश करूंगा:

limits = plt.axis("on")

ताकि आप धुरी के साथ ड्रा का उपयोग कर सकें।