不同颜色的 Altair 选择
Altair selection with different colours
我今天开始玩 Altair,对选择有疑问。我创建了 3 个不同颜色的条形图,如下所示。现在,每当我单击一个条形图时,我都希望在其他两个条形图中突出显示等效项。如果我不使用任何颜色并且默认为蓝色,这会很好用,但我不知道如何在选择中保留我的颜色而不是所有 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')
当您写 alt.Color("#73a1eb:N")
时,这意味着您希望颜色根据名为 "#73a1eb"
的列进行编码,该列具有标称 ("N"
) 类型。
您似乎想要指定颜色值而不是颜色编码,在这种情况下您可以写alt.value("#73a1eb")
。所以你的情况看起来像这样:
color1 = alt.condition(selection,
alt.value("#73a1eb"),
alt.value('lightgray'))
我今天开始玩 Altair,对选择有疑问。我创建了 3 个不同颜色的条形图,如下所示。现在,每当我单击一个条形图时,我都希望在其他两个条形图中突出显示等效项。如果我不使用任何颜色并且默认为蓝色,这会很好用,但我不知道如何在选择中保留我的颜色而不是所有 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')
当您写 alt.Color("#73a1eb:N")
时,这意味着您希望颜色根据名为 "#73a1eb"
的列进行编码,该列具有标称 ("N"
) 类型。
您似乎想要指定颜色值而不是颜色编码,在这种情况下您可以写alt.value("#73a1eb")
。所以你的情况看起来像这样:
color1 = alt.condition(selection,
alt.value("#73a1eb"),
alt.value('lightgray'))