Position altair legend top-center

Viewed 2260

I would like to position an altair legend top-center. Getting it to the top is simple enough by passing legend=alt.Legend(title=None, orient="top"). I also gather that this is possible in vega-lite but am having trouble making that all work together. `

1 Answers

Altair does not yet support the layout property that newer versions of Vega-Lite support, but you can position your legend manually following the approach used in one of the other answers to the linked question.

For example:

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'))
)

enter image description here

Related