代码之家  ›  专栏  ›  技术社区  ›  Olivier Ma

牵牛星:不能刻面分层的情节

  •  1
  • Olivier Ma  · 技术社区  · 7 年前

    我在报纸上读到 documentation 我可以分面绘制多层图,但不知何故,数据集中在输出图中,并在所有方面重复。

    我可以刻面每一层没有问题,这里是一个例子 cars 数据集:

    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

    但综合起来,所有的数据都会出现在每一块地上

    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 我做错什么了吗?

    1 回复  |  直到 7 年前
        1
  •  18
  •   jakevdp    7 年前

    这是因为有一个小问题,我们在文章的结尾进行了简短的讨论 Facet section 在文件里。

    LayerChart 对象作为父对象,每个 Chart 对象作为子对象。子级可以从父级继承数据,也可以指定自己的数据,在这种情况下,将忽略父级数据。

    .

    facet() 方法。下面是一个将这些放在一起的示例:

    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

    推荐文章