Altair facet charts save individual image

Viewed 101
1 Answers

You can only save full charts; there is no mechanism to individually save subpanels of a single chart.

As a workaround, you can re-create these subpanels as single charts. Here is one possible approach using a conditional opacity:

import altair as alt
from vega_datasets import data
iris = data.iris()

chart = alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    width=160,
    height=160
)

for species in ['setosa', 'versicolor', 'virginica']:
    chart.encode(
        opacity=alt.condition(
            f"datum.species == '{species}'", alt.value(1), alt.value(0)
        )
    ).display()

enter image description here

enter image description here

enter image description here

Related