[Python] Altair choropleth map selection interval and bidirectional linking

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I want to make a bidirectional plot where the selected values through brush in the scatter plot appear in a different opacity in the choropleth map, and the selected countries in the choropleth appear in a different color in the scatter plot. It appears that the geoshape does not accept brushing, so I tried to overlay a "hidden" dots map but it also does not work. Any help is greatly appreciated. Thank you!

    selection = alt.selection_point(fields=['income_group'], bind='legend')
    brush = alt.selection_interval()

    # code the choropleth map
    choro_map = alt.Chart(countries).mark_geoshape(stroke='black', strokeWidth=0.2).transform_lookup(
    lookup="properties.name",
    from_=alt.LookupData(
    final_country,
    key="country",
    fields=[
    "log_billionaires_100_million",
    "number_billionaires",
    "billionaires_per_100_million",
    "income_group", "country"
    ])
    ).transform_filter(brush).encode(
    color=alt.condition(
    selection & (alt.datum.log_billionaires_100_million > 0),
    alt.Color(
    "log_billionaires_100_million:Q",
    scale=alt.Scale(scheme="tealblues", domain=[0.9, 8]),
    title=["Log Number of", "Billionaires", "Per 100 Million"],
    legend=alt.Legend(offset=0)), alt.value("lightgray")),
    opacity=alt.condition(
    brush,
    alt.value(1),
    alt.value(0.3)
    )).project("mercator").add_params(selection, brush)

    dots_country = alt.Chart(final_country).mark_circle(opacity=0).encode(
    longitude= 'longitude_country:Q',
    latitude= 'latitude_country:Q',
    size = alt.when(brush).then(alt.value(1)).otherwise(alt.value(5))
    ).properties(width = 500, height = 250).add_params(brush)

    scatter_plot = alt.Chart(final_country).transform_filter("datum.income_group != null").mark_circle(size=50).encode(
    alt.X("total_tax_rate_country:Q", title="Total Tax Rate (%)"),
    alt.Y("tax_revenue_country_country:Q", title="Tax Revenue (%)"),
    alt.Size("billionaires_per_100_million:Q", title="Billionaires per 100M"),
    color = alt.condition(brush, alt.Color(
    "income_group:N",
    title="Income Group",
    scale=alt.Scale(scheme="set2")), alt.value("lightgray")),
    opacity=alt.when(selection).then(alt.value(1)).otherwise(alt.value(0.2)),
    ).add_params(selection, brush)

    alt.vconcat((choro_map+dots_country), scatter_plot, center = True)

    Continue reading...

Compartilhe esta Página