简体   繁体   English

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

[英]Python - Altair - Stacked Bar Chart With Selection

I've followed and replicated with my dataset two tutorials, linked below 我已经按照我的数据集复制了两个教程,并在下面链接

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

and

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

I'm having difficulty understanding if Altair is capable of combining these two, though. 不过,我很难理解Altair是否能够将这两者结合起来。

Is it possible to have a stacked bar graph, where each "subsection" of the graph is selectable. 是否可以有一个堆积的条形图,其中该图的每个“子部分”都是可选的。 So, something like if I had data that was like 所以,如果我有类似的数据

Category1, Category2

in bars, and each one can have subsections 在酒吧里,每个可以有小节

Sub1, Sub2

I'd have a stacked bar graph where say the Categroy1/Category2 parts that are related to Sub1 are Blue, the parts related to Sub2 are Orange, and I can select any of the 4 parts (Cat1+Sub1, Cat1+Sub2, Cat2+Sub1, Cat2+Sub2, represented as 2 stacked bars) and that part then turns Red. 我会有一个堆叠的条形图,其中与Sub1相关的Categroy1 / Category2零件为蓝色,与Sub2相关的零件为橙色,我可以选择4个零件中的任何一个(Cat1 + Sub1,Cat1 + Sub2,Cat2 + Sub1,Cat2 + Sub2,表示为2个堆叠的条形),然后该部分变为红色。

Is this possible or out-of-scope, and if possible, what am I missing conceptually? 这是可能的还是范围之外的?如果可能,我在概念上想念的是什么?

Yes, this is possible. 是的,这是可能的。 You can specify in the selector which encodings you would like it to respond to; 您可以在选择器中指定您要响应的编码。 to respond to individual sections of the stacked bar, specify x and color . 要响应堆叠条的各个部分,请指定xcolor

Here's an example: 这是一个例子:

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
)

在此处输入图片说明

click here to try it live in the vega editor. 点击此处在vega编辑器中进行尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM