简体   繁体   English

Python:Altair Condition 颜色不透明度

[英]Python: Altair Condition Color Opacity

In an Altair condition I want to specify the opacity for the the secondary conditional color.在 Altair 条件下,我想指定辅助条件颜色的不透明度。 My data in the scatter are fairly dense so I want the unselected points to go away (more or less).我的散点数据相当密集,所以我希望未选择的点消失(或多或少)。

Using the example from here , I want this code: 使用此处的示例,我想要以下代码:

selection = alt.selection_multi(fields=['Origin'])
color = alt.condition(selection,
                   alt.Color('Origin:N', legend=None),
                   alt.value('lightgray') # WANT THIS TO BE %50 OPACITY
)

scatter = alt.Chart(cars).mark_point().encode(
 x='Horsepower:Q',
 y='Miles_per_Gallon:Q',
 color=color,
 tooltip='Name:N'
)

legend = alt.Chart(cars).mark_point().encode(
 y=alt.Y('Origin:N', axis=alt.Axis(orient='right')),
 color=color
).add_selection(
 selection
)

scatter | legend

to have the color condition be like:使颜色条件如下:

color = alt.condition(selection,
                   alt.Color('Origin:N', legend=None),
                   alt.Color(value='lightgray', opacity=0.5)
)

However I can't seem to figure it out and there doesn't appear to be any solution online.但是我似乎无法弄清楚,而且网上似乎没有任何解决方案。

Thanks!谢谢!

The opacity is a separate channel, so you can put a condition on both the color and the opacity:不透明度是一个单独的通道,因此您可以对颜色和不透明度设置条件:

color = alt.condition(selection,
                      alt.Color('Origin:N', legend=None),
                      alt.value('lightgray'))
opacity = alt.condition(selection, alt.value(1.0), alt.value(0.5))

scatter = alt.Chart(cars).mark_point().encode(
 x='Horsepower:Q',
 y='Miles_per_Gallon:Q',
 color=color,
 opacity=opacity,
 tooltip='Name:N'
)

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

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