簡體   English   中英

Altair 將選擇添加到分層圖表

[英]Altair add selection to Layered chart

我有下面的代碼。 但是“選擇”並沒有按預期工作。 當一個點被選中時,其他線被取消選中。 但所選線的點也會隱藏/消失。

我一定做錯了什么。 這是將選擇添加到分層圖表的正確方法嗎?

import altair as alt
from vega_datasets import data

source = data.stocks()

selection1 = alt.selection_single()

line = alt.Chart(source).mark_line().encode(
    x='date',
    y='price',
    #color= 'symbol',
    color=alt.condition(selection1, 'symbol', alt.value('grey')),
    opacity=alt.condition(selection1, alt.value(0.8), alt.value(0.1)),
)

point = line.mark_point(size = 40, fill='white')

alt.layer(line, point).add_selection(selection1)

默認情況下,選擇只選擇與您單擊的標記直接關聯的數據。 如果您希望它應用於更大的數據集,您可以指定fieldsencodings 在您的情況下,聽起來您希望它適用於具有相同symbol的所有數據,因此您可以這樣做:

selection1 = alt.selection_single(fields=['symbol'])

或者,由於您的符號在所有情況下都映射到顏色,因此您可以這樣做:

selection1 = alt.selection_single(encodings=['color'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM