我想要一个交互式图表,所以我首先定义
click = selection_multi(fields=['species'])
Run Code Online (Sandbox Code Playgroud)
在该encode()方法中,以下效果很好:
color = condition(click,
'species',
value('gray'))
Run Code Online (Sandbox Code Playgroud)
但我宁愿使用我自己的颜色palette,我不想要一个legend. 我可以通过以下方式实现这一点。
color = Color('species',
scale=Scale(range=palette),
legend=None)
Run Code Online (Sandbox Code Playgroud)
但现在我没有选择!我可以同时拥有它们吗?
要获得多项选择、您自己的调色板而不是图例,只需在里面指定所有这些 color().
工作代码
import altair as alt
from vega_datasets import data
iris = data.iris()
click = alt.selection_multi(fields=['species'])
palette = alt.Scale(domain=['setosa', 'versicolor', 'virginica'],
range=['lightgreen', 'darkgreen', 'olive'])
alt.Chart(iris).mark_point().encode(
x='petalWidth',
y='petalLength',
color=alt.condition(click,
'species:N', alt.value('lightgray'),
scale=palette,
legend=None)
).properties(
selection=click
)
Run Code Online (Sandbox Code Playgroud)
产生:
如果你点击任何一点,整个物种将根据颜色条件被选择和着色。(选定的点采用 的颜色,palette未选定的点显示为灰色。)