简体   繁体   English

不同颜色的 Altair 选择

[英]Altair selection with different colours

I started playing around with Altair today and have a question about selections.我今天开始玩 Altair,并且有一个关于选择的问题。 I have created 3 bar charts with different colours as shown below.我创建了 3 个不同颜色的条形图,如下所示。 Now whenever I click on one bar, I'd like the equivalent to be highlighted in the other two bar charts.现在,每当我单击一个条形图时,我希望在其他两个条形图中突出显示等效项。 This works well if I don't use any colour and it defaults to blue, but I can't figure out how to keep my colours in the selection instead of having the same colour for all 3 charts.如果我不使用任何颜色并且默认为蓝色,则此方法效果很好,但我无法弄清楚如何将我的颜色保留在选择中,而不是让所有 3 个图表的颜色都相同。

在此处输入图像描述

df = pd.DataFrame({"Name":["Bulbasaur", "Charizard", "Mewtwo"], 
                  "HP":[45, 80, 100], 
                  "Attack":[30, 50, 60], 
                  "Defense":[40, 38, 42], 
                  "Type":["Grass", "Fire", "Psychic"]}) 


selection = alt.selection_single(fields=["Type 1"])

# This doesn't do much as I don't have a column named #73a1eb. But it's the colour code I'd like to use.
color1 = alt.condition(selection,
                  alt.Color("#73a1eb:N", legend=None), 
                  alt.value('lightgray'))

color2 = alt.condition(selection,
                  alt.Color("#73b9c7:N", legend=None),
                  alt.value('lightgray'))

color3 = alt.condition(selection,
                  alt.Color("#d7abf5:N", legend=None),
                  alt.value('lightgray'))

# The mark bar colour is overriden by color in encode

chart1 = alt.Chart(df, title="Average HP by Type")
            .mark_bar(color="#73a1eb", size = 12)
            .encode(x = 'mean(HP):Q',
                    y = alt.Y('Type 1:N', sort='-x'), 
                    tooltip=["Type 1", "mean(HP):Q"], 
                    color=color1)
            .properties(height=320,
                        width=300)
            .add_selection(selection)

chart2 = alt.Chart(df, title="Average Attack by Type")
            .mark_bar(color="#73a1eb", size = 12)
            .encode(x = 'mean(Attack):Q',
                    y = alt.Y('Type 1:N', sort='-x'), 
                    tooltip=["Type 1", "mean(HP):Q"], 
                    color=color1)
            .properties(height=320,
                        width=300)
            .add_selection(selection)

chart2 = alt.Chart(df, title="Average Defense by Type")
            .mark_bar(color="#73a1eb", size = 12)
            .encode(x = 'mean(Defense):Q',
                    y = alt.Y('Type 1:N', sort='-x'), 
                    tooltip=["Type 1", "mean(HP):Q"], 
                    color=color1)
            .properties(height=320,
                        width=300)
            .add_selection(selection)

alt.hconcat(chart1, chart2, chart3).configure_axis(grid=False).configure_axisBottom().resolve_scale(x = 'shared')

When you write alt.Color("#73a1eb:N") , it means you want the color to be encoded according to a column named "#73a1eb" which has a nominal ( "N" ) type.当您编写alt.Color("#73a1eb:N")时,这意味着您希望根据名为"#73a1eb"的列进行编码,该列具有标称 ( "N" ) 类型。

It appears that you want to specify a color value rather than a color encoding, in which case you can write alt.value("#73a1eb") .您似乎想要指定颜色值而不是颜色编码,在这种情况下您可以编写alt.value("#73a1eb") So your condition would look like this:所以你的情况看起来像这样:

color1 = alt.condition(selection,
                       alt.value("#73a1eb"),
                       alt.value('lightgray'))

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

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