简体   繁体   English

如何使用时间滑块 plot 和 map 并使用 plotly 在 Z23EEEB4347BDD756BFC6B7EE9A 中放大城市

[英]How to plot a map with time-slider and zoom on a city with plotly in python

I want to create an interactive map that shows the evolution of a variable (number of bikes) on different points of a city.我想创建一个交互式 map,显示城市不同点上变量(自行车数量)的演变。

Why not by using plotly.为什么不使用 plotly。

I would like to do something like this: https://amaral.northwestern.edu/blog/step-step-how-plot-map-slider-represent-time-evolu with a slider.我想做这样的事情: https://amaral.northwestern.edu/blog/step-step-how-plot-map-slider-represent-time-evolu和 slider。

However, I don't achieve to reproduce it with focusing on a city, I can't choose a scope more precise than "europe".但是,我无法以城市为中心来重现它,我无法选择比“欧洲”更精确的 scope。

Do you know how to do it with a zoom?你知道如何用变焦来做到这一点吗?

  • you have not provided any code or sample data.您没有提供任何代码或示例数据。 Hence have used this http://api.citybik.es/v2/ https://github.com/eskerda/pybikes project to source some bike data因此使用了这个http://api.citybik.es/v2/ https://github.com/eskerda/pybikes项目来获取一些自行车数据
  • data is just latest, so build up data in a pickle file for evolution数据只是最新的,所以在pickle文件中建立数据以进行进化
  • simple case of using Plotly Express and animation_frame argument使用Plotly Expressanimation_frame参数的简单案例

data sources数据源

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")]

bike stand sourcing and plotting自行车架采购和绘图

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM