欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

osmnx 笔记: plot_graph_route plot_graph_routes

发布时间:2025/4/5 编程问答 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 osmnx 笔记: plot_graph_route plot_graph_routes 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

本身没什么复杂的,主要是处理数据的部分比较繁琐,所以就独立出来写一个了

1 数据集处理

1.1 获取graph

import osmnx as ox point1=(31.191184,121.516295) G_=ox.graph_from_point(point1,dist=3000,network_type='drive') ox.plot_graph(G_)

1.2 转化成GeoDataFrame

nodes_sh,edges_sh=ox.graph_to_gdfs(G_)

python 包介绍:osmnx_UQI-LIUWJ的博客-CSDN博客-6.1

 

1.3 提取edges_sh中的边 

u, v, _ = list(zip(*edges_sh.index)) edges_sh["u"] = u edges_sh["v"] = v #这样的话edges_sh 中就会有u和v 两列了edges_sh['id']=np.arange(edges_sh.shape[0]) edges_sh.set_index('id',inplace=True) #修改索引列edges_sh.drop(['oneway','highway','ref','bridge','lanes','maxspeed','geometry','tunnel'],axis=1,inplace=True) #丢弃一些暂时不用到的边

1.4 随机生成路线

route_=[63328951] #表示初始的点 for i in range(1000):try:route_.append(np.random.permutation(edges_sh[edges_sh['u']==route_[-1]]['v'].values)[0])except:break#u是起点,v是终点,每一次将上一个终点作为下一个起点#这里用try-except的意思是,如果到了边界,那么就不会有下一条u-v边了,结束route的更新

2 plot_graph_route

2.1 基本使用方法

osmnx.plot.plot_graph_route(G, route, route_color='r', route_linewidth=4, route_alpha=0.5, orig_dest_size=100, ax=None, **pg_kwargs)

2.2 参数说明

G (networkx.MultiDiGraph)输入图
route (list)点id组成的list
route_color (string) 路线的颜色
route_linewidth (int) 路线的宽度
route_alpha (float)路线的透明度
orig_dest_size (int) 起止点的大小
ax图像的轴

2.3 使用举例

ox.plot.plot_graph_route(G_,route_,route_color='green',route_linewidth=5,route_alpha=0.5,orig_dest_size=500,figsize=(50,20))

 终点在图的边缘处,没有下一条边了(事实上这个route也只有五百多条边)。

这也就是之前使用try语句的原因。

3 plot_graph_routes

3.1 基本使用方法

osmnx.plot.plot_graph_routes(G, routes, route_colors='r', route_linewidths=4, **pgr_kwargs)

3.2 参数介绍

G (networkx.MultiDiGraph)输入的图
routes (list)route的list
route_colors (string or list)

如果是string的话,就是所有的路径使用相同的颜色

如果是list的话,就是不同的路径不同的颜色

route_linewidths (int or list)

如果是int的话,就是所有的路径使用相同的宽度

如果是list的话,就是不同的路径不同的宽度

3.3 使用举例

route_list=[] #route的集合 for _ in range(5):route_=[np.random.choice(nodes_sh.index.values)]#表示初始的点for i in range(1000):try:route_.append(np.random.permutation(edges_sh[edges_sh['u']==route_[-1]]['v'].values)[0])except:breakroute_list.append(route_) ox.plot.plot_graph_routes(G_,route_list,route_colors=['green','red','purple','blue','orange'],route_linewidths=5,figsize=(50,20))

 

总结

以上是生活随笔为你收集整理的osmnx 笔记: plot_graph_route plot_graph_routes的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。