在 Altair 的每个方面子图中显示 x 和 y 标签

Show x and y labels in each facet subplot in Altair

我指的是

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

alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    width=180,
    height=180
).facet(
    facet='species:N',
    columns=2
)

这会生成一个包含 3 个子图的图,其中共享 x 轴和 y 轴。我希望每个子图都有自己的 x 和 y 标签(即使这是重复的)。我怎样才能做到这一点?

您可以使用 resolve_axis method, discussed in Scale and Guide Resolution:

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

alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    width=180,
    height=180
).facet(
    facet='species:N',
    columns=2
).resolve_axis(
    x='independent',
    y='independent',
)