【问题标题】:Drawing nodes with coordinates in correct position using NetworkX/Matplotlib使用 NetworkX/Matplotlib 绘制坐标位置正确的节点
【发布时间】:2015-12-05 23:12:25
【问题描述】:

我知道 x/y 轴是翻转的(柏林位于汉堡东南部),但我需要手动修复这个问题,还是 matplotlib/networkx 可以帮我解决这个问题?如果需要手动完成,有没有最好的方法?

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)

【问题讨论】:

  • 我认为您更深层次的问题是您将纬度视为“x”,将经度视为“y”。相反,。反转 x 和 y 轴不会改变这一点。相反,您需要绘制 long, lat 而不是 lat, long
  • 对于一般情况,使用nx.draw中的pos参数,见Plot NetworkX Graph with coordinates

标签: python matplotlib graph networkx


【解决方案1】:

你可以使用

from matplotlib import pyplot

pyplot.gca().invert_yaxis()
pyplot.gca().invert_xaxis()

【讨论】:

  • 好的,这太棒了,因为现在我知道必须翻转纬度/经度。谢谢!
【解决方案2】:

您可以在绘图前反转位置。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2023-02-07
    • 2017-10-09
    相关资源
    最近更新 更多