温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Altair selections with different x and y encodings
python vega altair

python - 使用不同的x和y编码的Altair选择

发布于 2020-03-27 11:30:35

我正在尝试根据传入的数据框和变量列表动态生成要在网页上显示的图表列表。master_chart应该具有一个时间元素,然后master_chart上的时间间隔选择将影响在其他产生的图表上“突出显示”或选择的内容。但是,当我这样做时,所有其他产生的图表上的选择会在彼此之间起作用,但是主图表上的选择不会被其他图表注册。当我将其他图表的x变量更改为时间元素时,主图表上的选择起作用,并且更改了其他所有图表上数据点的颜色,反之亦然。但是,当我将x编码更改为所需的值时,主图表上的任何选择都会取消选择次级图表上的所有点,例如(我可以https://ibb.co/56LSTwW)。我希望在其他图表上显示这些时间的所有数据,但它似乎没有用。我是否误解了复合高度间隔选择的功能。任何想法将不胜感激。

编辑后:编辑后,我用西雅图天气vega数据集重现了该示例。我的目标是要有一组交互式图表,其中一个图表上的选择将突出显示其他图表上的数据点。该函数返回其中包含一个图表的列表,并且该列表在我的网页上的Jinja模板中呈现。我已经能够在网页上绘制许多不同类型的图表,因此我知道我的问题是基于我对Altair的理解。当我在三个具有相同Y轴的图表中的任何一个上进行选择时,它会在所有其他图表上选择点,包括master_chart,例如https://ibb.co/ssDwGZ6但是当我在最上面的图master_chart上进行选择时,其他任何图上都没有选择https://ibb.co/pRwDbF4这是否与其他图表的for循环结构有关,还是源于主图表的x和y编码与其他图表不同?任何建议将不胜感激。

import altair as alt
import pandas as pd
from vega_datasets import data

var_dframe = data.seattle_weather()
primary_x = "precipitation"
param_list = ["precipitation", "temp_max", "temp_min", "wind"]

def make_charts(var_dframe, primary_x, param_list):
    other_charts_list = []
    compound_chart_list = []
    the_brush = alt.selection(type='interval', resolve='global')
    master_chart = alt.Chart(var_dframe).mark_point(filled=True).encode(
        x= alt.X("date:T"),
        y= primary_x +":Q",
        color= alt.condition(the_brush, alt.value('blue'), alt.value('grey'))
    ).add_selection(the_brush)
    for item in (param_list[1:]):
        new_chart = alt.Chart(var_dframe).mark_point(
            filled=True,
        ).encode(
            x= alt.X("%s:Q" % item),
            y= alt.Y("temp_max:Q"),
            color = alt.condition(the_brush, alt.value('blue'), alt.value('gray'))
        ).add_selection(the_brush)
        other_charts_list.append(new_chart)

    compound_chart = master_chart
    for chart in other_charts_list:
        compound_chart = compound_chart & chart

    compound_chart_list.append(compound_chart.to_json())
    return(compound_chart_list)


查看更多

查看更多

提问者
khb
被浏览
33
jakevdp 2019-07-04 04:08

这种奇怪的行为归因于Vega-Lite中的一个已知错误:时间数据的时间间隔选择返回空选择解决方法是向每个图表面板添加一个时间编码。在这里,您可以通过在代码段中创建的detail="date:T"每个new_chart对象中添加作为编码来实现此目的。