osmnx 笔记: plot_graph_route plot_graph_routes
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pandas (GeoPandas)笔记
- 下一篇: pandas 补充笔记:转换提取类型