在python中为osm图边缘着色
我正在尝试制作一张地图,其中某条路线(例如路线中包含的边缘)是彩色的。现在,我有以下代码:
node_route=[1223724841, 1688813022, 2464466873, 1688813022, 1688813022, 1688813022, 1688813022]
ec = ['w' for u, v, k in G_map.edges(keys=True)]
for i in range(len(node_route)-1):
ec = ['r' for u, v, k in G_map.edges(keys=True) if (u==node_route[i] and v==node_route[i+1])]
fig, ax = ox.plot_graph(G_map, node_color='r', node_edgecolor='k', node_size=40, node_zorder=3, edge_color=ec, edge_linewidth=1)
列表node_rote是我想要着色的有序路线。我首先将“w”分配给图表(G_map)中的每个边。然后我检查 node_route 列表中的每一对(索引 i 和 i+1),如果该对等于图形边缘的“u”和“v”,则它将用“r”着色。问题是这段代码返回一个空列表。
感谢您的帮助!
I'm trying to make a map where a certain route, e.g. the edges included in the route, is colored. For now, I have the following code:
node_route=[1223724841, 1688813022, 2464466873, 1688813022, 1688813022, 1688813022, 1688813022]
ec = ['w' for u, v, k in G_map.edges(keys=True)]
for i in range(len(node_route)-1):
ec = ['r' for u, v, k in G_map.edges(keys=True) if (u==node_route[i] and v==node_route[i+1])]
fig, ax = ox.plot_graph(G_map, node_color='r', node_edgecolor='k', node_size=40, node_zorder=3, edge_color=ec, edge_linewidth=1)
The list node_rote is the ordered route i would like to color. I start by assigning 'w' to every edge in my graph (G_map). Then i check every pair in my node_route list (index i and i+1) and if the pair is equal to 'u' and 'v' of the graph edges it will be colored with 'r'. The problem is this code returns an empty list.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
对于初学者来说,每次循环时都会重新分配 ec ,并丢弃之前的值。
如果我正确理解您要做什么,您最好构建一个字典,将节点 id:s 映射到它们在
node_route
中的位置。然后循环遍历G_map.edges(...)
,在node_routeu
和v
节点的位置code> 使用字典,并根据pos[u]+1 == pos[v]
是否将“r”或“w”附加到ec
中。For starters, you are re-assigning
ec
each time through the loop, throwing away the earlier value.If I understand correctly what you are trying to do, you might be better off building a dictionary that maps node id:s to their position in
node_route
. Then loop overG_map.edges(...)
, looking up the positions of theu
andv
nodes innode_route
using your dictionary, and appending 'r' or 'w' toec
depending on whetherpos[u]+1 == pos[v]
or not.