Plotly choropleth_mapbox 在解析 animation_frame="" 时不显示地图
Plotly choropleth_mapbox doesn't show map when parsing animation_frame=""
fig = px.choropleth_mapbox(Defined_M_rob_data, geojson=merged['geometry'],
color='Crime count',
color_continuous_scale = "viridis",
locations='EER13CD',
#animation_frame="Month",
center={"lat": 53.13258, "lon": -1.81356},
mapbox_style="carto-positron", zoom=5.3)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0,"pad":4})
fig.update_layout(
title={
'text': " Robbery in the North East and South West (03/20-02/21)",
'y':0.98,
'x':0.45,
'xanchor': 'auto',
'yanchor': 'auto'})
fig.show()
我已经尝试了多种方法。每次我可以注释掉 animation_frame,我就会在地图上得到我第一个月的数据密度。当我包括每个月要经历的动画(总共 13 个)时,我得到一个没有错误的勾号,告诉我代码 运行 成功,大约需要 2 分钟。
问题是地图根本不显示。我真的不知道我做错了什么,因为代码看起来很简单。
组合数据和地理文件的示例:
错误日志:
|Timestamp|Level|Message|
|---|---|---|
|Apr 1, 2022, 12:30:23 AM|INFO|Discarding 2 buffered messages for 6930931d-b4b1-46f9-b65c-6bba1ad894ed:6cfa772d26934f8deee6a74d78425003|
|Apr 1, 2022, 12:30:23 AM|INFO|Adapting to protocol v5\.1 for kernel 6930931d-b4b1-46f9-b65c-6bba1ad894ed|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.websocket.WebSocketClosedError|
|Apr 1, 2022, 12:30:22 AM|WARNING| raise WebSocketClosedError\()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 876, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING| yielded = self\.gen.throw(*exc_info)|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1141, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|During handling of the above exception, another exception occurred:|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.iostream.StreamClosedError: Stream is closed|
|Apr 1, 2022, 12:30:22 AM|WARNING| value = future\.result()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1133, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING| yield fut|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 874, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|future: \<Future finished exception=WebSocketClosedError()>|
|Apr 1, 2022, 12:30:22 AM|WARNING|ERROR:asyncio:Future exception was never retrieved|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.websocket.WebSocketClosedError|
|Apr 1, 2022, 12:30:22 AM|WARNING| raise WebSocketClosedError\()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 876, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING| yielded = self\.gen.throw(*exc_info)|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1141, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|During handling of the above exception, another exception occurred:|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.iostream.StreamClosedError: Stream is closed|
|Apr 1, 2022, 12:30:22 AM|WARNING| value = future\.result()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1133, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING| yield fut|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 874, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|future: \<Future finished exception=WebSocketClosedError()>|
|Apr 1, 2022, 12:30:22 AM|WARNING|ERROR:asyncio:Future exception was never retrieved|
|Apr 1, 2022, 12:30:22 AM|INFO|Starting buffering for 6930931d-b4b1-46f9-b65c-6bba1ad894ed:6cfa772d26934f8deee6a74d78425003|
- 找到这个 API 来获取犯罪数据 https://data.police.uk/docs/method/crime-street/
- 从您引用的地方(英格兰地区)获取几何图形
- 故意将几何简化为 1000m(先转换为 UTM,所以
simplify()
单位是米)
- 绘图代码使用简化的几何图形不变
绘制代码
import plotly.express as px
Defined_M_rob_data = (
df.groupby(["EER13CD", "month"], as_index=False)
.size()
.rename(columns={"size": "Crime count", "month": "Month"})
)
merged = gdf.set_index("EER13CD")
fig = px.choropleth_mapbox(
Defined_M_rob_data,
geojson=merged["geometry"],
color="Crime count",
color_continuous_scale="viridis",
locations="EER13CD",
animation_frame="Month",
center={"lat": 53.13258, "lon": -1.81356},
mapbox_style="carto-positron",
zoom=5.3,
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0, "pad": 4})
数据来源
import geopandas as gpd
import pandas as pd
import requests
gdf = gpd.read_file(
"https://raw.githubusercontent.com/martinjc/UK-GeoJSON/master/json/electoral/gb/topo_eer.json"
).set_crs("epsg:4386")
# simplify the geometry
gdf["geometry"] = gdf.to_crs(gdf.estimate_utm_crs()).simplify(1000).to_crs("epsg:4326")
df = pd.DataFrame()
# convex_hull gives a single polygon for each region. it will generate some overlaps, hence maybe some
# double counting...
for reg, geom in gdf.set_index("EER13CD").convex_hull.iteritems():
if reg[0] != "E":
continue
ps = ":".join([f"{y},{x}" for x, y in geom.exterior.coords])
for d in pd.date_range("1-oct-2021", "31-mar-2022", freq="MS").strftime("%Y-%m"):
params = {"poly": ps, "date": d}
res = requests.get(
"https://data.police.uk/api/crimes-street/robbery",
params=params,
)
if res.status_code == 200:
df = pd.concat(
[
df,
pd.json_normalize(res.json()).assign(EER13CD=reg),
]
)
else:
print(f"failed to get data {reg} {d}")
df.groupby(["EER13CD", "month"], as_index=False).size().rename(
columns={"size": "Crime count"}
)
fig = px.choropleth_mapbox(Defined_M_rob_data, geojson=merged['geometry'],
color='Crime count',
color_continuous_scale = "viridis",
locations='EER13CD',
#animation_frame="Month",
center={"lat": 53.13258, "lon": -1.81356},
mapbox_style="carto-positron", zoom=5.3)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0,"pad":4})
fig.update_layout(
title={
'text': " Robbery in the North East and South West (03/20-02/21)",
'y':0.98,
'x':0.45,
'xanchor': 'auto',
'yanchor': 'auto'})
fig.show()
我已经尝试了多种方法。每次我可以注释掉 animation_frame,我就会在地图上得到我第一个月的数据密度。当我包括每个月要经历的动画(总共 13 个)时,我得到一个没有错误的勾号,告诉我代码 运行 成功,大约需要 2 分钟。
问题是地图根本不显示。我真的不知道我做错了什么,因为代码看起来很简单。
组合数据和地理文件的示例:
|Timestamp|Level|Message|
|---|---|---|
|Apr 1, 2022, 12:30:23 AM|INFO|Discarding 2 buffered messages for 6930931d-b4b1-46f9-b65c-6bba1ad894ed:6cfa772d26934f8deee6a74d78425003|
|Apr 1, 2022, 12:30:23 AM|INFO|Adapting to protocol v5\.1 for kernel 6930931d-b4b1-46f9-b65c-6bba1ad894ed|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.websocket.WebSocketClosedError|
|Apr 1, 2022, 12:30:22 AM|WARNING| raise WebSocketClosedError\()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 876, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING| yielded = self\.gen.throw(*exc_info)|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1141, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|During handling of the above exception, another exception occurred:|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.iostream.StreamClosedError: Stream is closed|
|Apr 1, 2022, 12:30:22 AM|WARNING| value = future\.result()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1133, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING| yield fut|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 874, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|future: \<Future finished exception=WebSocketClosedError()>|
|Apr 1, 2022, 12:30:22 AM|WARNING|ERROR:asyncio:Future exception was never retrieved|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.websocket.WebSocketClosedError|
|Apr 1, 2022, 12:30:22 AM|WARNING| raise WebSocketClosedError\()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 876, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING| yielded = self\.gen.throw(*exc_info)|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1141, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|During handling of the above exception, another exception occurred:|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.iostream.StreamClosedError: Stream is closed|
|Apr 1, 2022, 12:30:22 AM|WARNING| value = future\.result()|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1133, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING| yield fut|
|Apr 1, 2022, 12:30:22 AM|WARNING| File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 874, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|future: \<Future finished exception=WebSocketClosedError()>|
|Apr 1, 2022, 12:30:22 AM|WARNING|ERROR:asyncio:Future exception was never retrieved|
|Apr 1, 2022, 12:30:22 AM|INFO|Starting buffering for 6930931d-b4b1-46f9-b65c-6bba1ad894ed:6cfa772d26934f8deee6a74d78425003|
- 找到这个 API 来获取犯罪数据 https://data.police.uk/docs/method/crime-street/
- 从您引用的地方(英格兰地区)获取几何图形
- 故意将几何简化为 1000m(先转换为 UTM,所以
simplify()
单位是米) - 绘图代码使用简化的几何图形不变
绘制代码
import plotly.express as px
Defined_M_rob_data = (
df.groupby(["EER13CD", "month"], as_index=False)
.size()
.rename(columns={"size": "Crime count", "month": "Month"})
)
merged = gdf.set_index("EER13CD")
fig = px.choropleth_mapbox(
Defined_M_rob_data,
geojson=merged["geometry"],
color="Crime count",
color_continuous_scale="viridis",
locations="EER13CD",
animation_frame="Month",
center={"lat": 53.13258, "lon": -1.81356},
mapbox_style="carto-positron",
zoom=5.3,
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0, "pad": 4})
数据来源
import geopandas as gpd
import pandas as pd
import requests
gdf = gpd.read_file(
"https://raw.githubusercontent.com/martinjc/UK-GeoJSON/master/json/electoral/gb/topo_eer.json"
).set_crs("epsg:4386")
# simplify the geometry
gdf["geometry"] = gdf.to_crs(gdf.estimate_utm_crs()).simplify(1000).to_crs("epsg:4326")
df = pd.DataFrame()
# convex_hull gives a single polygon for each region. it will generate some overlaps, hence maybe some
# double counting...
for reg, geom in gdf.set_index("EER13CD").convex_hull.iteritems():
if reg[0] != "E":
continue
ps = ":".join([f"{y},{x}" for x, y in geom.exterior.coords])
for d in pd.date_range("1-oct-2021", "31-mar-2022", freq="MS").strftime("%Y-%m"):
params = {"poly": ps, "date": d}
res = requests.get(
"https://data.police.uk/api/crimes-street/robbery",
params=params,
)
if res.status_code == 200:
df = pd.concat(
[
df,
pd.json_normalize(res.json()).assign(EER13CD=reg),
]
)
else:
print(f"failed to get data {reg} {d}")
df.groupby(["EER13CD", "month"], as_index=False).size().rename(
columns={"size": "Crime count"}
)