按位置对 osmnx 地图进行着色(使用 graph_from_place() 方法)
Color osmnx maps by place (using graph_from_place() method)
我们绘制了 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)
正如我们在下面的输出中看到的,街道已根据其长度进行了着色。
我的问题:我们如何根据地点而非长度为街道着色?那么第一个村庄的所有街道都是一种颜色,而另一个村庄的所有街道都是另一种颜色?
与length相反,data
对象中不包含该地方,我不知道如何检索它。
这是一个模拟示例,我特意选择了小村庄。实际上我想用一系列整个城市来着色。
- 能够根据地点为边缘着色的核心是了解地点
- 已使用获取每个位置的 osmnx* 图表并为其分配位置的方法
- 那么使用 geopandas / folium 集成到颜色边缘就很简单了
- 还显示了似乎是首选的方式 matplotlib
Matplotlib
# 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)
我们绘制了 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)
正如我们在下面的输出中看到的,街道已根据其长度进行了着色。
我的问题:我们如何根据地点而非长度为街道着色?那么第一个村庄的所有街道都是一种颜色,而另一个村庄的所有街道都是另一种颜色?
与length相反,data
对象中不包含该地方,我不知道如何检索它。
这是一个模拟示例,我特意选择了小村庄。实际上我想用一系列整个城市来着色。
- 能够根据地点为边缘着色的核心是了解地点
- 已使用获取每个位置的 osmnx* 图表并为其分配位置的方法
- 那么使用 geopandas / folium 集成到颜色边缘就很简单了
- 还显示了似乎是首选的方式 matplotlib
Matplotlib
# 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)