Altair: Can't facet layered plots

孤人 提交于 2020-05-12 16:08:10

问题


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

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

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

Am I doing something wrong?


回答1:


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')



来源:https://stackoverflow.com/questions/52872927/altair-cant-facet-layered-plots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!