Python 是否可以创建到datalist元素的Altair绑定而不是select?
我试图在时间序列图上设置一个基因表达的交互式过滤器。创建此类过滤器的文档化方法是将select_single绑定到输入表单。对于数量较少的选项,可以使用绑定选择。例如Python 是否可以创建到datalist元素的Altair绑定而不是select?,python,plot,interactive,vega-lite,altair,Python,Plot,Interactive,Vega Lite,Altair,我试图在时间序列图上设置一个基因表达的交互式过滤器。创建此类过滤器的文档化方法是将select_single绑定到输入表单。对于数量较少的选项,可以使用绑定选择。例如 import altair as alt group_dropdown = alt.binding_select(options=gene_names) group_select = alt.selection_single(fields=['gene'], bind=group_dropdown, name='Feature',
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个可以选择的基因,所以下拉式绑定并不是一个真正的选择。一个元素将是完美的。上的vega lite文档意味着我应该能够使用任何HTML表单输入元素,但我无法确定映射到该元素的Altair类。这是可能的,但由于两个原因有些困难: 尽管Vega支持任意参数来形成输入,但Vega lite的模式禁止此类参数。这意味着您需要围绕Altair的正常验证机制来使用它。 必须将输入注入图表的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))
这是可能的,但有点困难,原因有二: 尽管Vega支持任意参数来形成输入,但Vega lite的模式禁止此类参数。这意味着您需要围绕Altair的正常验证机制来使用它。 必须将输入注入图表的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))