Olivier Ma
Olivier Ma

Reputation: 1309

Altair: Can't facet layered plots

I read in the documentation that I can facet a multi-layer plot, but somehow the data are lumped together in output plot, and repeated in all facets.

I can facet each layer with no problem, here is an example with the cars dataset:

import altair as alt
from altair import datum
from vega_datasets import data
cars = data.cars()

horse = alt.Chart(cars).mark_point().encode(
    x = 'Weight_in_lbs',
    y = 'Horsepower'
)

chart = alt.hconcat()
for origin in cars.Origin.unique():
    chart |= horse.transform_filter(datum.Origin == origin).properties(title=origin)
chart

enter image description here

miles = alt.Chart(cars).mark_point(color='red').encode(
    x = 'Weight_in_lbs',
    y = 'Miles_per_Gallon'
)

chart = alt.hconcat()
for origin in cars.Origin.unique():
    chart |= miles.transform_filter(datum.Origin == origin).properties(title=origin)
chart

enter image description here

But when combined all the data show up in every plot

combined = horse + miles

chart = alt.hconcat()
for origin in cars.Origin.unique():
    chart |= combined.transform_filter(datum.Origin == origin).properties(title=origin)
chart

enter image description here Am I doing something wrong?

Upvotes: 14

Views: 7779

Answers (1)

jakevdp
jakevdp

Reputation: 86310

This is due to a little gotcha that's discussed very briefly at the end of the Facet section in the docs.

You can think of a layered chart in Altair as a hierarchy, with the LayerChart object as the parent, and each of the individual Chart objects as children. The children can either inherit data from the parent, or specify their own data, in which case the parent data is ignored.

Now, because you specified data for each child chart separately, they ignore any data or transform coming down from the parent. The way to get around this is to specify the data only in the parent.

As a side note, Altair also has a shortcut for the manual filtering and concatenating you're using here: the facet() method. Here is an example of putting this all together:

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

horse = alt.Chart().mark_point().encode(
    x = 'Weight_in_lbs',
    y = 'Horsepower'
)

miles = alt.Chart().mark_point(color='red').encode(
    x = 'Weight_in_lbs',
    y = 'Miles_per_Gallon'
)

alt.layer(horse, miles, data=cars).facet(column='Origin')

enter image description here

Upvotes: 28

Related Questions