重复图表

Repeated charts

我正在尝试制作一个情节:

import altair as alt
from vega_datasets import data

movies = data.movies.url

base = alt.Chart(movies).mark_bar().encode(
alt.Y('count()')).properties(
    width=200,
    height=150
)

chart = alt.vconcat()
for x_encoding in ['IMDB_Rating:Q', 'IMDB_Votes:Q']:
    row = alt.hconcat()
    for maxbins_encoding in [10, 50]:
        row |= base.encode(alt.X(x_encoding, 
        type='quantitative',
        bin=Bin(maxbins=maxbins_encoding)))
    chart &= row
chart

这行得通。然后我尝试使用 alt.repeat():

alt.Chart(vega_datasets.data.movies.url).mark_bar().encode(
    alt.X(alt.repeat("row"), type='quantitative',  
    bin=Bin(maxbins=alt.repeat('column'))),
    alt.Y('count()')
).properties(
    width=200,
    height=150
).repeat(
    row=['IMDB_Rating', 'IMDB_Votes'],
    column=[10, 50]
)

它给我这个错误信息:

SchemaValidationError: Invalid specification

        altair.vegalite.v3.schema.core.BinParams->maxbins, validating 'type'

        {'repeat': 'column'} is not of type 'number'

所以我一定是漏掉了什么。它与在 bin=Bin() 参数中使用 repeat() 而不是直接在 encode() 中使用它有什么关系吗?

遗憾的是,重复条目不能用于 bin 参数。在 Vega-Lite 中使用 repeat 的唯一参数是传递给编码的列名,因此您的初始循环方法可能是最好的。

如果你想利用 x 编码的重复,你可以这样做:

def make_column(maxbins):
    return alt.Chart(movies).mark_bar().encode(
        alt.X(alt.repeat("row"), type='quantitative',  
              bin=alt.Bin(maxbins=maxbins)),
        alt.Y('count()')
    ).properties(
        width=200,
        height=150
    ).repeat(
        row=['IMDB_Rating', 'IMDB_Votes'],
    )

make_column(10) | make_column(50)