Altair中的世界地图+滑块(Python) [英] World map + slider in Altair (Python)

查看:148
本文介绍了Altair中的世界地图+滑块(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python中的Altair库与世界地图一起构建一个交互式图表,其中每个国家和日期的COVID-19确认病例数和日期滑块.

I would like to build an interactive chart with world map with COVID-19 confirmed cases per country and date slider using Altair library in python.

数据格式:

"country_region","date","confirmed_cases"
"Afghanistan",2020-01-22,0
"Afghanistan",2020-01-23,0
"Afghanistan",2020-01-24,0
"Afghanistan",2020-01-25,0

我设法建立了非交互式图表,但是无法设置滑块.现在,我的图表如下所示:

I have managed to build non-interactive chart, but can't set up slider. My chart now looks like this:

我想添加滑块以选择日期,但是无法正确设置日期.

I want to add slider to chose date, but can't set up it properly.

我使用transform_lookup方法根据数据(确定的病例/人口)向国家/地区添加颜色,而据我了解,transform_filter应该添加,以便在用户更改滑块位置时执行数据过滤.但这是行不通的.我的假设是,在transform_lookup中始终使用原始数据,并且过滤在那里不起作用.我没有找到有关同时使用transform_lookup和滑块的示例或文档.

I use transform_lookup method to add colors to countries according to the data (confirmed cases / population) and transform_filter as far as I understand should be added to perform data filtering when slider position is changed by user. But this doesn't work. My assumption is that in transform_lookup always original data is used and filtering doesn't work there. I haven't found examples or documentation about using transform_lookup and slider at the same time.

很高兴听到任何想法可以帮助我解决这个问题.

Would be grateful to hear any ideas what can help me with this problem.

代码:

import requests
import json

import pandas as pd

import altair as alt
from vega_datasets import data
from altair import datum

df = pd.read_csv('https://raw.githubusercontent.com/MariaKokshaikina/any-data/main/covid19_global_confirmed_cases%20(1).csv')

country_info = requests.get(
    'https://raw.githubusercontent.com/MariaKokshaikina/any-data/main/country_info.json'
).json()

df = df[df['country_region'].isin(country_info)]
df = df.sort_values('date', ascending=True)
df = df.tail(5000)

def timestamp(t):
    return pd.to_datetime(t).timestamp() * 1000

df['id'] = df['country_region'].map(lambda x: country_info[x]['numericCode'])
df['rate'] = df['confirmed_cases'] / df['country_region'].map(lambda x: country_info[x]['population'])
df['timestamp'] = df['date'].map(timestamp)

countries = alt.topo_feature(data.world_110m.url, 'countries')

slider = alt.binding_range(
    step=24 * 60 * 60 * 1000,
    min=df['timestamp'].min(), 
    max=df['timestamp'].max()
)

select_date = alt.selection_single(
    name="slider", 
    fields=['timestamp'],
    bind=slider, 
)

alt.Chart(countries).mark_geoshape()\
    .encode(color='rate:Q')\
    .add_selection(select_date)\
    .transform_filter(select_date)\
    .transform_lookup(
        lookup='id',
        from_=alt.LookupData(df, key='id', fields=['rate'])
    )\
    .project('equirectangular')\
    .properties(
        width=500,
        height=300,
        title='Title'
    )

  • 您可以删除.transform_filter(select_date)\行以使用不会改变任何内容的滑块查看工作地图.
    • You can remove .transform_filter(select_date)\ line to see working map with slider which doesn't change anything.
    • 推荐答案

      问题似乎是在Vega中,查找转换不会根据选择动态地重新计算.您可以通过切换作为主要数据源来解决此问题,以便所有时间戳都显示在最终的联接数据集中:

      The problem appears to be that in Vega, lookup transforms do not dynamically recompute in response to selections. You can address this by switching which is the primary data source, so that all timestamps appear in the final joined dataset:

      alt.Chart(df).mark_geoshape()\
          .encode(color='rate:Q')\
          .add_selection(select_date)\
          .transform_filter(select_date)\
          .transform_lookup(
              lookup='id',
              from_=alt.LookupData(countries, key='id',
                                   fields=["type", "properties", "geometry"])
          )\
          .project('equirectangular')\
          .properties(
              width=500,
              height=300,
              title='Title'
          )
      

      这篇关于Altair中的世界地图+滑块(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆