Drawing nodes with coordinates in the correct position using NetworkX / Matplotlib

I know that the x / y axis is upside down (Berlin is southeast of Hamburg), but do I need to fix it manually or can I use matplotlib / networkx for me? And if you need to do it manually, is there a better way to do this?

import networkx as nx

G = nx.Graph()

G.add_node('Hamburg', pos=(53.5672, 10.0285))
G.add_node('Berlin', pos=(52.51704, 13.38792))

nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, node_size=0)

enter image description here

+4
source share
2 answers

you can use

from matplotlib import pyplot

pyplot.gca().invert_yaxis()
pyplot.gca().invert_xaxis()
+2
source

You can invert positions before plotting.

pos = {city:(long, lat) for (city, (lat,long)) in nx.get_node_attributes(G, 'pos').items()}
nx.draw(G, pos, with_labels=True, node_size=0)

, nx.get_node_attributes('pos') . (city, (lat, long)), , pos, pos[city]=(long,lat).

+3

Source: https://habr.com/ru/post/1606655/


All Articles