我正在尝试在时序图上设置基因表达的交互式过滤器。创建此类过滤器的记录方法是将select_single
绑定到输入表单。对于较小数量的选项,可以使用binding_select
。例如
import altair as alt
group_dropdown = alt.binding_select(options=gene_names)
group_select = alt.selection_single(fields=['gene'], bind=group_dropdown, name='Feature', init={'gene': gene_names[0]})
filter_group = chart.add_selection(group_select).transform_filter(group_select)
但是,我可以选择约50K个基因,因此下拉列表(binding_select
)并不是真正的选择。 <datalist>
元素将是完美的。 Input Binding上的vega-lite文档暗示我应该能够使用任何HTML表单输入元素,但是我不知道可以映射到该元素的Altair类。>
答案 0 :(得分:0)
这是可能的,但是由于两个原因有些困难:
<datalist>
注入到图表的HTML输出中,并且没有很好的机制。以下是如何解决这些限制并在Altair选择输入绑定中使用数据列表的示例:
from IPython.display import HTML, display
import altair as alt
from vega_datasets import data
from altair.utils.display import HTMLRenderer
from altair.utils import schemapi
datalist = """
<datalist id="origin">
<option value="USA">
<option value="Europe">
<option value="Japan">
</datalist>
"""
# Allow specifications that are invalid according to the schema.
# This prevents a validation error for the `list` argument below.
schemapi.DEBUG_MODE = False
# `list` here should match the ID of the <datalist> specification.
widget = alt.binding(input='text', name='Country', list='origin')
# now create the chart as normal:
selection = alt.selection_single(fields=['Origin'], bind=widget)
color = alt.condition(selection,
alt.Color('Origin:N', legend=None),
alt.value('lightgray'))
chart = alt.Chart(data.cars.url).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color=color,
tooltip='Name:N'
).add_selection(
selection
)
# Note the following assumes the default renderer.
alt.renderers.enable('default')
# Render the chart to HTML without validating it against the schema:
renderer = alt.renderers.get()
html = renderer(chart.to_dict(validate=False))['text/html']
# Now display the datalist and chart rendering:
display(HTML(datalist + html))