简体   繁体   English

在 Altair 中向 Choropleth(带滑块)添加动态工具提示和标题

[英]Adding dynamic tooltip and title to Choropleth (with slider) in Altair

In a previous post , @jakevdp produced the very nice choropleth in Altair using a slider (pasted below for convenience):上一篇文章中,@jakevdp 使用 slider 在 Altair 中制作了非常漂亮的 choropleth(为方便起见,粘贴在下面):

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
)

I'm new to Altair, and wanted to understand how to make a few modifications to this plot:我是 Altair 的新手,想了解如何对此 plot 进行一些修改:

  1. How can we add a title to this plot that varies with the slider value ie "Number of Pills in year {slider_value_here}"?我们如何为这个 plot 添加一个标题,该标题随 slider 值而变化,即“一年中的药丸数 {slider_value_here}”?
  2. How can we change the slider label to be customized to yr rather than the current year_year value?我们如何将 slider label 更改为yr而不是当前的year_year值?
  3. How can we add a hover tooltip to each county ie fips value, with the following values, {year: {slider_year_value}, fips: {fips_code_value_on_hover}, number_pills: {Pill_per_pop}}?我们如何将 hover 工具提示添加到每个县,即 fips 值,具有以下值,{year: {slider_year_value}, fips: {fips_code_value_on_hover}, number_pills: {Pill_per_pop}}?
  4. How can we add an animation widget to this slider eg have a play button that automatically varies the slider value (where the timing can be controlled in the code)?我们如何向这个 slider 添加一个 animation 小部件,例如有一个播放按钮,可以自动改变 slider 值(可以在代码中控制时间)?

Any help with the modifications to this code would be extremely helpful.任何有关修改此代码的帮助都将非常有帮助。 I did try inserting tooltip in various places to make query 3 work, but couldn't make it happen.我确实尝试在各个地方插入tooltip以使查询 3 工作,但无法实现。

Any help appreciated任何帮助表示赞赏

Thanks for including a clear and reproducible example.感谢您提供一个清晰且可重复的示例。 Just a heads up for the future that it can be a good idea to break your bullet points into separate question so that it is easier for others to find them when searching, I don't believe everything you want is achievable in Altair/Vegalite currently: but here is my best attempt at answering:只是对未来的提醒,将您的项目符号分成单独的问题可能是一个好主意,以便其他人在搜索时更容易找到它们,我不相信您想要的一切都可以在 Altair/Vegalite 中实现:但这是我回答的最佳尝试:

  1. How can we add a title to this plot that varies with the slider value ie "Number of Pills in year {slider_value_here}"?我们如何为这个 plot 添加一个标题,该标题随 slider 值而变化,即“一年中的药丸数 {slider_value_here}”?

    I found this comment mentioning that this is not possible for axis titles , so I would believe it is not possible for chart titles either (not sure though).我发现这个评论提到这对于轴标题是不可能的,所以我相信图表标题也不可能(虽然不确定)。 An alternative approach would be to insert a small chart instead of a title and use mark_text to update the year, similar to this example另一种方法是插入一个小图表而不是标题并使用mark_text更新年份, 类似于此示例

  2. How can we change the slider label to be customized to yr rather than the current year_year value?我们如何将 slider label 更改为 yr 而不是当前的 year_year 值?

    Set the name parameter in the binding_range instead of the selection_single , eg slider = alt.binding_range(name='yr ', min=2006, max=2012, step=1) (extra space for not having the slider right next to the name.binding_range中设置 name 参数而不是selection_single ,例如slider = alt.binding_range(name='yr ', min=2006, max=2012, step=1) (在名称旁边没有 slider 的额外空间.

  3. How can we add a hover tooltip to each county ie fips value, with the following values, {year: {slider_year_value}, fips: {fips_code_value_on_hover}, number_pills: {Pill_per_pop}}?我们如何将 hover 工具提示添加到每个县,即 fips 值,具有以下值,{year: {slider_year_value}, fips: {fips_code_value_on_hover}, number_pills: {Pill_per_pop}}?

    First change the lookup to also fetch the fips column:首先更改查找以获取 fips 列:

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

    Then pass a list to tooltip indicating the column types since they come from the lookup and are not in in the main dataframe you passed to the chart.然后将一个列表传递给工具提示,指示列类型,因为它们来自查找并且不在您传递给图表的主 dataframe 中。

     tooltip=['year:O', 'fips:Q', 'Pill_per_pop:Q']
  4. How can we add an animation widget to this slider eg have a play button that automatically varies the slider value (where the timing can be controlled in the code)?我们如何向这个 slider 添加一个 animation 小部件,例如有一个播放按钮,可以自动改变 slider 值(可以在代码中控制时间)?

    Animations are not yet supported in vegalite vegalite 尚不支持动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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