【问题标题】:Show only specific facet in Altair Python在 Altair Python 中仅显示特定方面
【发布时间】:2020-02-28 14:57:17
【问题描述】:

使用 Altair,我只想为给定的方面绘制特定的图。例如,使用 Vega 数据集,我想仅绘制欧洲的汽车。我知道如何使用 facet 分隔 3 个图,但还没有弄清楚如何只显示一个图。

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

这导致欧洲、日本和美国的 3 行图。我将如何只显示其中之一或三分之二?

【问题讨论】:

    标签: python facet altair


    【解决方案1】:

    您可以使用filter transform 来限制图表中显示的数据。例如,这将方面限制在美国和欧洲:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2021-05-17
      • 2016-11-12
      • 1970-01-01
      相关资源
      最近更新 更多