当前,我有一个图显示了网络中所有节点与目标之间的所有最短路径:现在,我想制作一个cmap,在其中根据最短路径的距离为原点节点和边缘着色。谁能帮我吗?
这是我所拥有的:
import networkx as nx
import matplotlib.pyplot as plt
import osmnx as ox
import pandas as pd
import geopandas as gpd
from shapely.wkt import loads as load_wkt
ox.config(log_console=True, use_cache=True)
place = {'city': 'Lisbon', 'country': 'Portugal'}
G = ox.graph_from_place(place, network_type='drive')
G = ox.project_graph(G)
hospitals = ox.pois_from_place(place, amenities=['hospital'])
hosp_1 = hospitals.iloc[21]['geometry'] # Hospital Santa Maria (Polygon)
def poly_centroide(polygon):
# Gives me the coordinates of the center point of the Polygon
p1 = load_wkt(polygon)
centroide = p1.centroid.wkt
return centroide
polygon_1 = str(hosp_1)
coord_1_str = poly_centroide(polygon_1)
coord_1 = (38.74817825481225, -9.160815118526642) # Coordinates Hospital Santa Maria
target_1 = ox.get_nearest_node(G, coord_1)
routes = []
for node in G.nodes:
try:
route = nx.shortest_path(G, node, target_1)
routes.append(route)
except nx.exception.NetworkXNoPath:
continue
fig, ax = ox.plot_graph_routes(G, routes, edge_linewidth=0.2, node_size=5, route_linewidth=1)
plt.show()
现在,我想知道如何创建cmap,其中节点和边缘的颜色基于最短路径的距离。
我怀疑可以用nx.dra()完成,但我不知道如何...
谢谢你。
我已在您的代码中稍加添加。这将有助于根据节点的拓扑距离为节点着色(由于在计算最短路径时您没有通过任何特定的权重,因此,最短路径是根据到达每个目的地所需遍历的边数来计算的。分配的权重为1)。