You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Altair多行分面图中实现跨行显示选择框?

问题:Altair跨多行图表同步高亮选中区域

需求:创建跨多行的交互式图表,选中某一行的x轴子区域时,所有行的对应区域高亮(非选中区域变为灰色)。当前代码仅能让选中行显示绿色背景,无法实现所有行同步高亮。

原代码:

import altair as alt
from vega_datasets import data

source = data.stocks()

brush = alt.selection_interval(encodings=["x"], mark=alt.BrushConfig(fill="green"))

histogram = (
    alt.Chart(source)
    .mark_bar()
    .encode(
        x="date:T",
        y="price:Q",
        color=alt.condition(brush, "symbol:N", alt.value("gray"), legend=None),
    )
)
   
overlay = (
    alt.Chart(source)
    .mark_rect(opacity=0.8)
    .encode(
        x="date:T",
        y="proce:Q",
        color=alt.condition(brush, alt.value("green"), alt.value("gray"), legend=None),
    )
)
(histogram + overlay).properties(height=50, width=400).facet(
    row=alt.Row("symbol:N",)
).transform_filter(alt.datum.symbol != "GOOG").add_selection(brush)

解决方案

修正后的代码实现了跨所有行同步高亮选中区域:

import altair as alt
from vega_datasets import data

source = data.stocks()

# 设置全局共享的brush选择,跨所有facet面板生效
brush = alt.selection_interval(encodings=["x"], resolve='global', mark=alt.BrushConfig(fill="green", opacity=0.2))

histogram = (
    alt.Chart(source)
    .mark_bar()
    .encode(
        x="date:T",
        y="price:Q",
        # 选中时保留symbol的分类颜色,未选中时统一为灰色
        color=alt.condition(brush, "symbol:N", alt.value("gray"), legend=None),
    )
)
   
overlay = (
    alt.Chart(source)
    .mark_rect(opacity=0.2)  # 降低透明度避免遮挡柱状图
    .encode(
        x="date:T",
        # 让矩形覆盖整个面板高度,0和1对应面板的上下边界
        y=alt.value(0),
        y2=alt.value(1),
        # 选中区域显示绿色背景,未选中区域透明
        color=alt.condition(brush, alt.value("green"), alt.value("transparent")),
    )
    .add_selection(brush)
)

# 组合图表并设置行分面,过滤GOOG数据
(histogram + overlay).properties(height=50, width=400).facet(
    row=alt.Row("symbol:N")
).transform_filter(alt.datum.symbol != "GOOG")

关键修正点

  • 全局共享选择区间:为selection_interval添加resolve='global',让所有分面面板共用同一个brush选择,确保选中某一行的x区间后,所有行都能响应。
  • Overlay铺满整行:使用y=alt.value(0)和y2=alt.value(1)让高亮矩形填充整个面板高度,无需依赖price字段,保证每个行的高亮区域完整覆盖该行。
  • 修复拼写错误:原代码中y="proce:Q"是笔误,修正为price后才能正确渲染overlay。
  • 优化颜色逻辑:柱状图在未选中时统一为灰色,选中时保留原分类颜色;overlay仅在选中区域显示绿色背景,既突出选中区域又不遮挡柱状图的细节。

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 12:18:27