Is there a way to show Altair Tooltip all the time?

Viewed 43

I like the way tooltip looks way more than when I add text as labels for the points in my plot, is there a way to make it visible wether the mouse is on it or not?

I looked it up but haven't found any solutions, maybe with messing around with conditions?

example code from doc if you have ideas you'd like to test out :

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=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']
).interactive()

thank u :)

1 Answers

As per my comment above, I think the easiest way to do this is adding a styled text box. You can see an example of how to style it in this issue, which I also pasted below:

import altair as alt
from vega_datasets import data

cars = data.cars()
chart = alt.Chart(cars).mark_circle().encode(
        alt.X('Miles_per_Gallon', scale=alt.Scale(domain=(5,50))),
        y='Weight_in_lbs')
corl = cars[['Miles_per_Gallon','Weight_in_lbs']].corr().iloc[0,1]

text = alt.Chart({'values':[{}]}).mark_text(
    align="left", baseline="top"
).encode(
    x=alt.value(5),  # pixels from left
    y=alt.value(5),  # pixels from top
    text=alt.value([f"r: {corl:.3f}", 'Line 2']))

box = alt.Chart({'values':[{}]}).mark_rect(stroke='black', color='orange').encode(
    x=alt.value(3),
    x2=alt.value(50),
    y=alt.value(3),
    y2=alt.value(30))

chart + box + text + chart.transform_regression('Miles_per_Gallon','Weight_in_lbs').mark_line()

enter image description here

Related