将 altair legend 置于顶部中心

Position altair legend top-center

我想将 altair 图例置于顶部中心。通过传递 legend=alt.Legend(title=None, orient="top") 使其到达顶部非常简单。我还收集到 this is possible in vega-lite 但无法使它们一起工作。 `

A​​ltair 尚不支持较新版本的 Vega-Lite 支持的 layout 属性,但您可以按照链接的其他答案之一中使用的方法手动定位图例问题。

例如:

import altair as alt
from vega_datasets import data

source = data.cars()

alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color=alt.Color('Origin', legend=alt.Legend(
        orient='none',
        legendX=130, legendY=-40,
        direction='horizontal',
        titleAnchor='middle'))
)