![](/img/trans.png)
[英]How to disable zoom in plotly choropleth mapbox plot with Python?
[英]How to plot a map with time-slider and zoom on a city with plotly in python
我想創建一個交互式 map,顯示城市不同點上變量(自行車數量)的演變。
為什么不使用 plotly。
我想做這樣的事情: https://amaral.northwestern.edu/blog/step-step-how-plot-map-slider-represent-time-evolu和 slider。
但是,我無法以城市為中心來重現它,我無法選擇比“歐洲”更精確的 scope。
你知道如何用變焦來做到這一點嗎?
animation_frame
參數的簡單案例import requests
import pandas as pd
from pathlib import Path
import plotly.express as px
# avalaible data sources...
pd.json_normalize(
requests.get("http://api.citybik.es/v2/networks").json()["networks"]
).loc[lambda d: d["location.city"].eq("London")]
df = pd.json_normalize(
requests.get("http://api.citybik.es/v2/networks/santander-cycles").json()[
"network"
]["stations"]
)
# build up some data over time
df["timestamp"] = pd.to_datetime(df["timestamp"]).round("15min")
f = Path.cwd().joinpath("bikes.pickle")
if not f.exists():
df.to_pickle(f)
else:
df = pd.concat([pd.read_pickle(f), df])
df = df.groupby(["timestamp","id"], as_index=False).first()
df.to_pickle(f)
# now just plot it on a map with evolution by time
df["ts_str"] = df["timestamp"].dt.strftime("%d-%b %H:%M")
px.scatter_mapbox(
df,
lat="latitude",
lon="longitude",
size="free_bikes",
hover_data=["name"],
animation_frame="ts_str",
).update_layout(
mapbox={"style": "carto-positron", "zoom":11}, margin={"l": 0, "r": 0, "t": 0, "b": 0}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.