You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone,
I’m working on an Altair facet chart where each facet represents a country, and inside each facet, I display three key metrics:
Military spending (spend)
Population (pop)
GDP (bip)
I want to allow users to dynamically sort the country facets based on one of these three metrics via a dropdown menu. However, while the sorting parameter updates correctly, the facet order does not change dynamically as expected.
Here’s my code:
`import altair as alt
import pandas as pd
Sample Data: Military spending, population, and GDP per country
chart = alt.Chart(df_melted).mark_bar().encode(
x="value:Q",
y=alt.Y("metric:N", title=""), # Show Spend, Population, and GDP as categories
color="metric:N" # Different colors for each metric
).facet(
row=alt.Row(
"country:N",
sort=alt.EncodingSortField(field="sort_order", order="descending") # Sort dynamically
)
).add_params(facet_sort_param
).transform_lookup(
lookup="country", from_=alt.LookupData(df, "country", ["spend", "pop", "bip"]) # Assign values for sorting
).transform_calculate(
sort_order=f"datum[{facet_sort_param.name}]" # Apply dynamic sorting
)
chart.show()`
The dropdown updates the sorting parameter, but the facet order remains unchanged.
I expected the facets (countries) to reorder dynamically based on the selected metric (spend, pop, bip), but it seems like Altair does not apply dynamic sorting to facets.
Question:
Is there a way to make Altair update the facet order dynamically when selecting a different sorting metric?
If Altair does not support this natively, is there a workaround to achieve dynamic facet sorting?
Any insights would be much appreciated! Thanks in advance.
Best,
Georg
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everyone,
I’m working on an Altair facet chart where each facet represents a country, and inside each facet, I display three key metrics:
I want to allow users to dynamically sort the country facets based on one of these three metrics via a dropdown menu. However, while the sorting parameter updates correctly, the facet order does not change dynamically as expected.
Here’s my code:
`import altair as alt
import pandas as pd
Sample Data: Military spending, population, and GDP per country
df = pd.DataFrame({
"country": ["USA", "China", "Russia", "India", "Germany", "UK"],
"spend": [801, 293, 65, 76, 56, 68], # Military Spending (Billion USD)
"pop": [331, 1441, 143, 1393, 83, 67], # Population (Million people)
"bip": [23, 17, 1.5, 3, 4.5, 3.1], # GDP (Trillion USD)
})
Convert data from wide to long format for Altair
df_melted = df.melt(id_vars=["country"], var_name="metric", value_name="value")
Sorting dropdown for facets
facet_sort_dropdown = alt.binding_select(
options=["spend", "pop", "bip"], name="Sort Countries by: "
)
facet_sort_param = alt.param(value="spend", bind=facet_sort_dropdown)
Chart with dynamically sorted facets
chart = alt.Chart(df_melted).mark_bar().encode(
x="value:Q",
y=alt.Y("metric:N", title=""), # Show Spend, Population, and GDP as categories
color="metric:N" # Different colors for each metric
).facet(
row=alt.Row(
"country:N",
sort=alt.EncodingSortField(field="sort_order", order="descending") # Sort dynamically
)
).add_params(facet_sort_param
).transform_lookup(
lookup="country", from_=alt.LookupData(df, "country", ["spend", "pop", "bip"]) # Assign values for sorting
).transform_calculate(
sort_order=f"datum[{facet_sort_param.name}]" # Apply dynamic sorting
)
chart.show()`
The dropdown updates the sorting parameter, but the facet order remains unchanged.
I expected the facets (countries) to reorder dynamically based on the selected metric (spend, pop, bip), but it seems like Altair does not apply dynamic sorting to facets.
Question:
Is there a way to make Altair update the facet order dynamically when selecting a different sorting metric?
If Altair does not support this natively, is there a workaround to achieve dynamic facet sorting?
Any insights would be much appreciated! Thanks in advance.
Best,
Georg
Beta Was this translation helpful? Give feedback.
All reactions