Altair SchemaValidationError: Invalid specification

Altair SchemaValidationError: Invalid specification

我制作了这张图表:

使用此代码:

q2 =  alt.Chart(source, width=100, height=100).mark_line().encode(
        y=alt.Y("prob:Q", title = ''),
        x=alt.X("year:O", title = ''),
        row=alt.Row("key1:N", title="Probability of "),
        column=alt.Column("key2:N", title="Given..."),
    )

但是我需要每一行出现“给定...”和“概率”,而不仅仅是一次,并且年份显示在每个图表的 x 轴上。它应该是这样的:

这是我正在使用的数据框:

我知道这需要一定程度的串联,但我不确定如何将这个概念应用到我的图表中。我试过这个:

charts = []
for key in totest:
    data = source[source['key1'] == key]
    chart = alt.Chart(data).mark_line().encode(
    y=alt.Y("prob:Q", title = ''),
    x=alt.X("year:O", title = ''),
    row=alt.Row("key1:N", axis=alt.Axis(title=['Probability of', key])),
    facet = alt.Facet("key2:N", sort=totest, title = "Given...")
            )
charts.append(chart)
return alt.vconcat(*charts)

但是我得到这个错误:

SchemaValidationError: Invalid specification

        altair.vegalite.v4.schema.channels.Row, validating 'additionalProperties'

        Additional properties are not allowed ('axis' was unexpected)
        

alt.VConcatChart(...)

错误不是特别清楚,但问题是 Vega-Lite 渲染器不支持多面图表的连接。除了显示多个图表而不是连接它们之外,我不知道有什么好的解决方法来做你想做的事:

for key in totest:
  data = source[source['key1'] == key]
  chart = alt.Chart(data).mark_line().encode(
    y=alt.Y("prob:Q", title = ''),
    x=alt.X("year:O", title = ''),
    row=alt.Row("key1:N", axis=alt.Axis(title=['Probability of', key])),
    facet = alt.Facet("key2:N", sort=totest, title = "Given...")
  )
  chart.display()