Reputation: 935
Currently I have a plot that shows all the shortest paths between all the nodes in my network and my target: Now I would like to make a cmap, where I would color the origin nodes and the edges based on the distance of the shortest path. Can anyone help me?
Here is what I have:
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()
Now I would like to know how to create the cmap where the colors of the nodes and edges are based on the distance of the shortest path.
I suspect it could be done with nx.dra() but I have no idea how...
Thank you in advance.
Upvotes: 2
Views: 926
Reputation: 950
I have slightly added to your code. This will help in colouring the nodes based on their topological distance (since you did not pass any specific weight while calculating the shortest path, the shortest path is calculated based on the number of edges needed to be traversed to reach the destination as each edge is assigned a weight of 1).
I start after target_1 = ox.get_nearest_node(G, coord_1)
Obtain the nodes and edges geodataframes from the graph. We need the nodes geodataframe for this purpose.
nodes, edges = ox.graph_to_gdfs(G, nodes=True, edges=True)
We then calculate the shortest path, the shortest path length, and assign the latter to a new column in the nodes
geodataframe.
nodes['shortest_route_length_to_target'] = 0
routes = []
route_lengths = []
i = 0
for node in G.nodes:
try:
route = nx.shortest_path(G, node, target_1)
route_length = nx.shortest_path_length(G, node, target_1)
routes.append(route)
route_lengths.append(route_length)
nodes['shortest_route_length_to_target'][node] = route_length
except nx.exception.NetworkXNoPath:
continue
Now we define the following functions. You will notice that these functions are the ones already existing in the file plot.py but are slightly modified for this purpose.
import numpy as np
import matplotlib.cm as cm
def get_colors(n, cmap='viridis', start=0., stop=1., alpha=1.,):
colors = [cm.get_cmap(cmap)(x) for x in np.linspace(start, stop, n)]
colors = [(r, g, b, alpha) for r, g, b, _ in colors]
return colors
def get_node_colors_by_attr(G, attr, num_bins=None, cmap='viridis', start=0, stop=1, na_color='none'):
if num_bins is None:
num_bins=len(G.nodes())
bin_labels = range(num_bins)
# attr_values = pd.Series([data[attr] for node, data in G.nodes(data=True)])
attr_values = pd.Series(nodes[attr].values)
cats = pd.qcut(x=attr_values, q=num_bins, labels=bin_labels)
colors = get_colors(num_bins, cmap, start, stop)
node_colors = [colors[int(cat)] if pd.notnull(cat) else na_color for cat in cats]
return node_colors
Now the following lines of code will give you your desired output.
nc = get_node_colors_by_attr(G, attr = 'shortest_route_length_to_target', num_bins=20,)
fig, ax = ox.plot_graph(G, node_color = nc, fig_height=20,)
You could vary the colormap (cmap
) or the number of bins (num_bins
) you wish to discretise the route_lengths
values into.
Upvotes: 1