good morning everyone,
I'm learning to graph in Python 2.7, but I have no idea how to graph a connected tree in python. I tried to do it in the following way, but the result is not what I expected.
Dataframe EDGE
Node1 Coord_X1 Coord_Y1 Node2 Coord_X2 Coord_Y2 distancia
185345.0 773597.6169 9962982.575 185346.0 773614.916 9963027.287 47.9418585885
185345.0 773597.6169 9962982.575 185347.0 773614.7503 9962997.751 22.8880836154
185345.0 773597.6169 9962982.575 186566.0 773613.8039 9963065.571 84.5597716719
185345.0 773597.6169 9962982.575 185351.0 773655.4929 9963006.051 62.4560161399
Dataframe NODE
ID Coord_X Coord_Y
186566 773613.8039 9963065.571
185342 773555.1688 9963035.582
189946 773634.7926 9963058.732
185346 773614.9160 9963027.287
189945 773583.1306 9963059.069
189943 773517.5156 9963059.012
189944 773552.3801 9963059.070
185351 773655.4929 9963006.051
185345 773597.6169 9962982.575
186557 773553.1549 9963101.634
185341 773554.5726 9962997.507
185347 773614.7503 9962997.751
185340 773551.2906 9962981.867
185331 773475.4813 9962983.065
#-----------------------------------------
import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
g1 = nx.Graph()
for i, elrow1 in BORDE.iterrows():
g1.add_edge(elrow1[0], elrow1[3])
#BORDE un dataframe de nodos y distancias entre nodos
for i, nlrow1 in NODO.iterrows():
g1.node[nlrow1['ID']] = nlrow1[1:].to_dict()
#NODO dataframe de nodos
#%%
print('# of edges: {}'.format(g1.number_of_edges()))
print('# of nodes: {}'.format(g1.number_of_nodes()))
#%%
node_positions1 = {node[0]: (node[1]['Coord_X'], -node[1]['Coord_Y']) for node in g1.nodes(data=True)}
dict(list(node_positions1.items())[0:7])
#%%
plt.figure(figsize=(8, 6))
nx.draw(g1, pos=node_positions1, edge_color='blue', node_size=15, node_color='black')
plt.title('Grafico de todos los caminos', size=15)
plt.show()
I try to get a graph like this,
Knowing the adjacency matrix.
Thank you for your cooperation.
Cheers
1 Answers