Search code examples
pythonaltair

Better way to create a tooltip in Altair


Is there a better way to create this tooltip in Altair? It doesn't feel very python-esque. Can I avoid the repetition of the alt.Tooltip? ('coin' is a str, 'usdValue' is numeric).

            tooltip=[
                alt.Tooltip("coin"),
                alt.Tooltip("usdValue", format="$.3s", title="Valor"),
            ],

Solution

  • You only need to use alt.Tooltip on the fields where you want to change the default tooltip formatting:

    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='Origin',
        tooltip=[
            'Origin',
            alt.Tooltip('Horsepower', format="$.3s", title="Valor")
        ]
    )
    

    And just a reminder to post a full minimal reproducible example with code and data for your next question.