如何根据选择的点(Altair)更新直方图?

时间:2019-10-16 18:49:15

标签: python altair

我的目标是根据左侧1左侧的点选择来更新右侧显示的直方图。 最初,该图看起来还不错,但是一旦做出选择,就不会重绘直方图(Altair 3.2.0)

有人知道该怎么做吗?

hist should be updated upon selection of the points on the left

下面是重新创建示例的代码:

import altair as alt
import pandas as pd
import numpy as np
from random import choice

dates = pd.date_range("1.1.2019", "2.28.2019")
np.random.seed(999)
Y = np.random.normal(0.5, 0.1, len(dates))
features = [choice(["a", "b", "c"]) for i in range(len(dates))]
df = pd.DataFrame({"dates": dates, "Y": Y, "features": features})

base = alt.Chart(df)
area_args = {"opacity": 0.3, "interpolate": "step"}
pts = alt.selection(type="interval", encodings=["x"])
points = (
    base.mark_circle()
    .encode(alt.X("dates:T"), alt.Y("Y:Q"), color="features:N")
    .add_selection(pts)
)

yscale = alt.Scale(domain=(0, 1))
right_hist = (
    base.mark_area(**area_args)
    .encode(
        alt.Y(
            "Y:Q", bin=alt.Bin(maxbins=20, extent=yscale.domain), stack=None, title=""
        ),
        alt.X("count()", stack=None, title=""),
        alt.Color("features:N"),
    )
    .transform_filter(pts)
)

(points | right_hist)

edit1 :另一张图片来阐明我的观点@jvp

enter image description here

1 个答案:

答案 0 :(得分:0)

在评论中解决了 OP 设置问题以及最终如何渲染图。