有人尝试过使用日期作为 Altair 交互式散点图的滑块吗?
我正在尝试重现与 gapminder 散点图类似的图:
1)我尝试使用日期而不是年份过滤器,例如“2020-01-05”并出现以下错误:
altair.vegalite.v4.schema.core.BindRange->max, validating 'type'
'2020-05-17T00:00:00' is not of type 'number'
2)当我尝试将其解析为 int 时,图中没有显示任何内容
3) 使用年份滑块的示例:https://www.datacamp.com/community/tutorials/altair-in-python https://www.datacamp.com/community/tutorials/altair-in-python
https://altair-viz.github.io/gallery/multiple_interactions.html https://altair-viz.github.io/gallery/multiple_interactions.html4)时间戳选项也不是理想的选择,因为日期需要可读
将不胜感激任何帮助。谢谢
#Date Slider
from altair import datum
from datetime import datetime
import altair as alt
import pandas as pd
import numpy as np
import datetime as dt
date_slider = alt.binding_range(min=min(df['date']), max=max(df['date']), step=1)
slider_selection = alt.selection_single(bind=date_slider, fields=['date'], name="Date", init={'week_starting': max(df[‘date’]})
alt.Chart(df).mark_point(filled=True).encode(
x='mom_pct',
y='yoy_pct',
size='n_queries',
color='vertical',
tooltip = ['vertical', 'yoy_pct', 'mom_pct']
).properties(
width=800,
height=600
).add_selection(slider_selection).transform_filter(slider_selection)