问题描述
在ggplot2
中,可以轻松创建具有跨越行和列的构面的构面图.在altair
中是否有巧妙"的方法来做到这一点? facet
文档
In ggplot2
, it's easy to create a faceted plot with facets that span both rows and columns. Is there a "slick" way to do this in altair
? facet
documentation
可以在单个列中绘制构面,
It's possible to have facets plot in a single column,
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'
)
但是,通常,我只想使用一个以上的列/行将它们绘制在一个网格中,即那些排列在单个列/行中的那些并没有特别的意义.
but often, I just want to plot them in a grid using more than one column/row, i.e. those that line up in a single column/row don't mean anything in particular.
例如,请参见ggplot2
中的facet_wrap
: http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/#facetwrap
推荐答案
在Altair 3.1版或更高版本(2019年6月发布)中,直接在Altair API中支持包装的构面.修改虹膜示例,您可以将构面包装在两列中,如下所示:
In Altair version 3.1 or newer (released June 2019), wrapped facets are supported directly within the Altair API. Modifying your iris example, you can wrap your facets at two columns like this:
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
)
或者,可以使用构面指定相同的图表作为编码:
Alternatively, the same chart can be specified with the facet as an encoding:
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参数.
The columns argument can be similarly specified for concatenated charts in alt.concat()
and repeated charts alt.Chart.repeat()
.
这篇关于Altair中的多列/行多面包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!