Reputation: 217
Given the image below some values are smaller than 50. I want values smaller than 50 not to be on the chart.
Is it possible to hide, remove or shrink opacity for this?
Code is:
import altair as alt
from vega_datasets import data
import streamlit as st
source=data.barley()
bars = alt.Chart(source).mark_bar().encode(
x=alt.X('sum(yield):Q', stack='zero'),
y=alt.Y('variety:N'),
color=alt.Color('site')
)
text = alt.Chart(source).mark_text(dx=-15, dy=3, color='white').encode(
x=alt.X('sum(yield):Q', stack='zero'),
y=alt.Y('variety:N'),
detail='site:N',
text=alt.Text('sum(yield):Q', format='.1f')
)
st.altair_chart(bars + text, theme="streamlit", use_container_width=True)
Upvotes: 1
Views: 459
Reputation: 48909
You can use an aggregate transform to have access to the aggregated values in a condition and filter based on a threshold:
import altair as alt
from vega_datasets import data
base = alt.Chart(data.barley())
bars = base.mark_bar().encode(
x=alt.X('sum(yield):Q', stack='zero'),
y=alt.Y('variety:N'),
color=alt.Color('site'),
)
text = base.mark_text(dx=-2, color='white', align='right').transform_aggregate(
yield_sum='sum(yield)',
groupby=['variety', 'site']
).encode(
x=alt.X('yield_sum:Q', stack='zero'),
y=alt.Y('variety:N'),
text=alt.Text('yield_sum:Q', format='.0f'),
opacity=alt.condition('datum.yield_sum > 50', alt.value(1), alt.value(0)),
order='site' # This is needed because the transform_aggregate return a different order of the values than the bar chart
)
bars + text
Upvotes: 1