python - Altair 中的多列/行小平面环绕

标签 python plot facet facet-wrap altair

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
)

enter image description here

或者,可以使用 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/

相关文章:

mongodb - 在 MongoDB Spring Data 中使用多个方面

python - 成对统一列表项

python - 如何在 tkinter 窗口中绘制图像

使用 kbhit 的 Python 套接字线程

python - 如何绘制非方形 Seaborn jointplot 或 JointGrid

r - 如何将单独的coord_cartesian()应用于 "zoom in"到facet_grid()的各个面板中?

python - Python Pandas 中 DataFrame 中的多索引的难题?

r - R中的多项式拟合和绘制回归线

c# - 在 ILNumerics 中制作一个实心框(立方体)

python - Altair:如何根据最大值在面网格中对线条进行不同的样式设计?