[英]Color osmnx maps by place (using graph_from_place() method)
We plot 2 places:我们plot 2个地方:
# Get data
import osmnx as ox
place = ["Broughton Hackett", "Crowle"]
G = ox.graph_from_place(place, retain_all=True, simplify = True, network_type='all')
# Prepare data
u = []
v = []
key = []
data = []
for uu, vv, kkey, ddata in G.edges(keys=True, data=True): # added ww
u.append(uu)
v.append(vv)
key.append(kkey)
data.append(ddata)
# Define colors according to length of streets
roadCols = []
for item in data:
if "length" in item.keys():
if item["length"] <= 200:
color = "#FFFFF0"
elif item["length"] > 200 and item["length"] <= 1000:
color = "#6a0dad"
else:
color = "#00FFFF"
else:
color = "#FFFFFF"
roadCols.append(color)
#Plot
bgcolor = "#1e1e1e"
fig, ax = ox.plot_graph(G, node_size=0,
dpi = 100,bgcolor = bgcolor,
save = False, edge_color=roadCols,
edge_linewidth=1, edge_alpha=1)
fig.tight_layout(pad=0)
fig.savefig("stack.jpg", dpi=100, bbox_inches='tight', format="jpg",
facecolor=fig.get_facecolor(), transparent=False)
As we can see in output below, streets have been colored according to their length.正如我们在下面的 output 中看到的,街道已根据其长度进行了着色。
My question: how do we color streets by place , not length?我的问题:我们如何按地点而不是长度为街道着色? So that all streets of the first village are in one color, and that all streets of the other village are in another color?
那么第一个村庄的所有街道都是一种颜色,而另一个村庄的所有街道都是另一种颜色?
Contrary to length, the place is not contained in the data
object, and I don't know how to retrieve it.与length相反,这个地方没有包含在
data
object中,不知道怎么找回。
This is a mock example and I have intentionally chosen tiny villages.这是一个模拟示例,我特意选择了小村庄。 In reality I want to color by a series of whole cities.
实际上,我想用一系列整个城市来着色。
# Get data
import osmnx as ox
import pandas as pd
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
plt.style.use('dark_background')
place = ["Broughton Hackett", "Crowle"]
gdf_nodes = gdf_edges = None
for place in place:
G = ox.graph_from_place(place, retain_all=True, simplify = True, network_type='all')
n_, e_ = ox.graph_to_gdfs(G)
n_["place"] = place
e_["place"] = place
if gdf_nodes is None:
gdf_nodes = n_
gdf_edges = e_
else:
gdf_nodes = pd.concat([gdf_nodes, n_])
gdf_edges = pd.concat([gdf_edges, e_])
# it's a bit imperfect way to map a value to a color
colors = {'Broughton Hackett':'red', 'Crowle':'blue'}
gdf_edges.plot(column="place", cmap=ListedColormap([colors[k] for k in sorted(colors.keys())]))
# Get data
import osmnx as ox
import pandas as pd
place = ["Broughton Hackett", "Crowle"]
gdf_nodes = gdf_edges = None
for place in place:
G = ox.graph_from_place(place, retain_all=True, simplify = True, network_type='all')
n_, e_ = ox.graph_to_gdfs(G)
n_["place"] = place
e_["place"] = place
if gdf_nodes is None:
gdf_nodes = n_
gdf_edges = e_
else:
gdf_nodes = pd.concat([gdf_nodes, n_])
gdf_edges = pd.concat([gdf_edges, e_])
gdf_edges.explore(column="place", height=300, width=500)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.