Karim
Karim

Reputation: 529

Show only specific facet in Altair Python

Using Altair I would like to plot only specific plots for a given facet. For example, using the Vega datasets, I would like to plot the cars only for Europe. I know how to separate the 3 plots using facet but have not managed to figure out how to show only one plot.

import altair as alt
from vega_datasets import data

source = data.cars()

chart = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
)

chart.facet(row='Origin')

This leads to 3 rows of plot for Europe, Japan and USA. How would I go about to show only one of these or two out of three?

Upvotes: 1

Views: 398

Answers (1)

jakevdp
jakevdp

Reputation: 86330

You can use a filter transform to limit the data that appears in your chart. For example, this limits the facets to USA and Europe:

import altair as alt
from vega_datasets import data

source = data.cars()

chart = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).transform_filter(
    (alt.datum.Origin == 'USA') | (alt.datum.Origin == 'Europe')
)
chart.facet(row='Origin')

enter image description here

Upvotes: 2

Related Questions