Search code examples
pythonaltairvega-lite

Can I turn altair axis titles into links?


In altair, possible to turn the axis titles into links?

(I understand how to use href for points, but not sure if I can do something similar for axis labels.)


Solution

  • I don't think it is possible to format axis title like that and don't see any property in the Vega-Lite docs that suggest you can. However, you could place a text mark strategically to receive the same result:

    import altair as alt
    from vega_datasets import data
    
    cars = data.cars()
    chart = alt.Chart(cars).mark_circle().encode(
            x=alt.X('Miles_per_Gallon', title=''),
            y='Weight_in_lbs',
            color='Origin'
    )
    
    text = alt.Chart().mark_text(
        align="center",
        baseline="top",
        fontSize=11,
        fontWeight=600,
        color='#007bff',
        href='https://stackoverflow.com'
    ).encode(
        x=alt.value(200),  # pixels from left
        y=alt.value(322),  # pixels from top
        text=alt.value("Miles_per_Gallon")
    )
    
    text + chart
    

    enter image description here