【问题标题】:Adding dynamic tooltip and title to Choropleth (with slider) in Altair在 Altair 中向 Choropleth(带滑块)添加动态工具提示和标题
【发布时间】:2021-02-01 05:26:04
【问题描述】:

previous post 中,@jakevdp 在 Altair 使用滑块(为方便起见粘贴在下方):

import altair as alt
import pandas as pd
from vega_datasets import data

us_counties = alt.topo_feature(data.us_10m.url, 'counties')
fdf = pd.read_csv('https://raw.githubusercontent.com/sdasara95/Opioid-Crisis/master/sample_data.csv')
fdf['year'] = fdf['year'].astype(str)
fdf = fdf.pivot(index='fips', columns='year', values='Pill_per_pop').reset_index()
columns = [str(year) for year in range(2006, 2013)]

slider = alt.binding_range(min=2006, max=2012, step=1)
select_year = alt.selection_single(name="year", fields=['year'],
                                   bind=slider, init={'year': 2006})

alt.Chart(us_counties).mark_geoshape(
    stroke='black',
    strokeWidth=0.05
).project(
    type='albersUsa'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(fdf, 'fips', columns)
).transform_fold(
    columns, as_=['year', 'Pill_per_pop']
).transform_calculate(
    year='parseInt(datum.year)',
    Pill_per_pop='isValid(datum.Pill_per_pop) ? datum.Pill_per_pop : -1'  
).encode(
    color = alt.condition(
        'datum.Pill_per_pop > 0',
        alt.Color('Pill_per_pop:Q', scale=alt.Scale(scheme='blues')),
        alt.value('#dbe9f6')
    )).add_selection(
    select_year
).properties(
    width=700,
    height=400
).transform_filter(
    select_year
)

我是 Altair 的新手,想了解如何进行一些修改 到这个情节:

  1. 我们如何为这个绘图添加一个随滑块值变化的标题 即“{slider_value_here} 年的药片数量”?
  2. 如何将滑块标签自定义为yr 而不是当前的year_year 值?
  3. 我们如何为每个县添加悬停工具提示,即 fips 值,如下所示 值,{年份:{slider_year_value},fips:{fips_code_value_on_hover},number_pills:{Pill_per_pop}}?
  4. 我们如何向此滑块添加动画小部件,例如有一个自动改变滑块值的播放按钮(可以在代码中控制时间)?

任何有关修改此代码的帮助都会非常有帮助。我确实尝试过 在各个地方插入 tooltip 以使查询 3 工作,但无法做到 发生。

任何帮助表示赞赏

【问题讨论】:

    标签: python altair choropleth


    【解决方案1】:

    感谢您提供清晰且可重复的示例!只是对未来的提醒,将您的要点分解为单独的问题可能是一个好主意,以便其他人在搜索时更容易找到它们。我不相信目前您想要的一切都可以在 Altair/Vegalite 中实现,但这是我回答的最佳尝试:

    1. 我们如何为该图添加一个随滑块值变化的标题,即“{slider_value_here} 年的药丸数”?

      我找到了this comment mentioning that this is not possible for axis titles,所以我相信图表标题也不可能(虽然不确定)。另一种方法是插入一个小图表而不是标题并使用mark_text 来更新年份,similar to this example

    2. 我们如何更改滑块标签以自定义为 yr 而不是当前 year_year 值?

      binding_range 中设置名称参数而不是 selection_single,例如slider = alt.binding_range(name='yr ', min=2006, max=2012, step=1)(名称旁边没有滑块的额外空间。

    3. 我们如何为每个县添加悬停工具提示,即 fips 值,具有以下值,{year: {slider_year_value}, fips: {fips_code_value_on_hover}, number_pills: {Pill_per_pop}}?

      首先将查找更改为也获取 fips 列:

       from_=alt.LookupData(fdf, 'fips', columns + ['fips'])
      

      然后将一个列表传递给工具提示,指示列类型,因为它们来自查找并且不在您传递给图表的主数据框中。

       tooltip=['year:O', 'fips:Q', 'Pill_per_pop:Q']
      
    4. 我们如何向此滑块添加动画小部件,例如有一个播放按钮可以自动改变滑块值(可以在代码中控制时间)?

      Animations are not yet supported in vegalite

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多