为列状 Altair 图表制作集体标题

Make collective title for columned Altair chart

美好的一天!

我有这个数据集,我从中制作了柱状条形图。

d = {'year': [2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017], 
     'num_pupils': [352, 365, 369, 438, 440, 456, 582, 584, 602], 
     'school_name': ["Brentwood", "Underburg", "Mary's House", 
                     "Brentwood", "Underburg", "Mary's House", 
                     "Brentwood", "Underburg", "Mary's House"]}

df = pd.DataFrame(data=d)

chart = alt.Chart(df).mark_bar().encode(
            alt.X('num_pupils:Q', title="No. of pupils"),
            alt.Y('year:N', title="Year", sort='descending'), 
            alt.Column('school_name:N', title= None)
        ).properties(
            title="Number of pupils per school from 2015 to 2017",
            width=250,
            height=250
        )

图表如下所示:

在底部,每一列都重复了“学生人数”——我想要一个标题并删除重复项。有办法吗?

感谢所有帮助,谢谢!

编辑:请参阅 debbes answer 以获得更简单的解决方法(如果您不需要 header 列作为构面)。

目前不支持此功能,但您可以通过手动放置 text_mark 来解决此问题,如 Can I turn altair axis titles into links?:

chart = alt.Chart(df).mark_bar().encode(
            alt.X('num_pupils:Q', title=""),
            alt.Y('year:N', title="Year", sort='descending'), 
            alt.Column('school_name:N', title= None)
        ).properties(
            title="Number of pupils per school from 2015 to 2017",
            width=250,
            height=250
        )
text = alt.Chart().mark_text(
    align="center",
    baseline="bottom",
    fontSize=11,
    fontWeight=600,
    dx=390,
    dy=0,
).encode(
    text=alt.value("No. of pupils")
)

(chart & text).configure_view(stroke=None)

您可以像这样使用 header 列:

chart = alt.Chart(df).mark_bar().encode(
            alt.X('num_pupils:Q', title=None),
            alt.Y('year:N', title="Year", sort='descending'), 
            alt.Column('school_name:N', header=alt.Header(title='No. of pupils', titleOrient='bottom')),
        ).properties(
            title="Number of pupils per school from 2015 to 2017",
            width=250,
            height=250
        )