按地点对 osmnx 地图进行着色(使用 graph_from_place() 方法)
我们绘制了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)
正如我们在下面的输出中看到的那样,街道已根据其长度进行着色。
我的问题:我们如何为 place 而不是长度彩色街道吗?因此,第一村的所有街道都采用一种颜色,而另一村的所有街道都采用另一种颜色?
与长度相反,该位置不包含在data
对象中,我不知道如何检索它。
这是一个模拟的例子,我有意选择了小村庄。实际上,我想通过一系列整个城市进行着色。
We plot 2 places:
# 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.
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.
This is a mock example and I have intentionally chosen tiny villages. In reality I want to color by a series of whole cities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Matplotlib
大叶
Matplotlib
folium