简体   繁体   中英

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?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM