Python-Altair-带有选择的堆积条形图

时间:2019-05-17 15:25:08

标签: python data-visualization stacked-area-chart altair

我已经按照我的数据集复制了两个教程,并在下面链接了

Stacked Bar Chart : https://altair-viz.github.io/gallery/stacked_bar_chart.html 

Selectable Data : https://altair-viz.github.io/gallery/interactive_cross_highlight.html 

我很难理解Altair是否能够将这两者结合起来。

有可能有一个堆叠的条形图,其中该图的每个“子部分”都是可选的。因此,就像我有类似的数据一样

Category1, Category2

以小节显示,每个小节都可以包含小节

Sub1, Sub2

我将有一个堆叠的条形图,其中与Sub1相关的Categroy1 / Category2零件为蓝色,与Sub2相关的零件为橙色,我可以选择4个零件中的任何一个(Cat1 + Sub1,Cat1 + Sub2,Cat2 + Sub1,Cat2 + Sub2,表示为2个堆叠的条形),然后该部分变为红色。

这是可能还是超出范围,如果可能的话,我在概念上缺少什么?

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。您可以在选择器中指定您要响应的编码。要响应堆积条的各个部分,请指定xcolor

这是一个例子:

import altair as alt
from vega_datasets import data

source = data.barley()

selector = alt.selection_single(encodings=['x', 'color'])

alt.Chart(source).mark_bar().encode(
    x='variety',
    y='sum(yield)',
    color=alt.condition(selector, 'site', alt.value('lightgray'))
).add_selection(
    selector
)

enter image description here

click here可以在vega编辑器中进行试用。