在 ggplot2
中,可以很容易地创建一个包含横跨行和列的分面的分面图。在 altair
中是否有一种“灵活”的方式来做到这一点? facet
documentation
可以在单个列中绘制分面,
import altair as alt
from vega_datasets import data
iris = data.iris
chart = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
).properties(
width=180,
height=180
).facet(
row='species:N'
)
在一行中,
chart = alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N'
).properties(
width=180,
height=180
).facet(
column='species:N'
)
但通常,我只是想使用不止一列/行将它们绘制在网格中,即那些排列在单个列/行中的那些并没有什么特别的意义。
例如,参见 ggplot2
中的 facet_wrap
:http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/#facetwrap
最佳答案
在 Altair 3.1 版或更高版本(2019 年 6 月发布)中,Altair API 直接支持包装面。修改您的虹膜示例,您可以像这样将您的构面包裹在两列中:
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
)
或者,可以使用 facet 指定相同的图表作为编码:
alt.Chart(iris).mark_point().encode(
x='petalLength:Q',
y='petalWidth:Q',
color='species:N',
facet=alt.Facet('species:N', columns=2)
).properties(
width=180,
height=180,
)
可以类似地为 alt.concat()
中的串联图表和 alt.Chart.repeat()
中的重复图表指定 columns 参数。
关于python - Altair 中的多列/行小平面环绕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50164001/