yogabonito
yogabonito

Reputation: 657

Slider label in Altair

When making an interactive chart with a slider using Altair 4.1, the slider's label is name_field (e.g. year_year as in the chart in this answer).

However, I have seen charts where this is not the case (e.g. in this question).

My slider labels always look like the first example. How can I get a slider label like the one in the second example (i.e. consisting of only one string and without an underscore)?

Upvotes: 2

Views: 1506

Answers (1)

jakevdp
jakevdp

Reputation: 86443

If you pass name to alt.binding_*, that will be the label used on the input widget. For example, Altair's documentation includes this chart:

import pandas as pd
import numpy as np

rand = np.random.RandomState(42)

df = pd.DataFrame({
    'xval': range(100),
    'yval': rand.randn(100).cumsum()
})

slider = alt.binding_range(min=0, max=100, step=1, name='cutoff:')
selector = alt.selection_single(name="SelectorName", fields=['cutoff'],
                                bind=slider, init={'cutoff': 50})

alt.Chart(df).mark_point().encode(
    x='xval',
    y='yval',
    color=alt.condition(
        alt.datum.xval < selector.cutoff,
        alt.value('red'), alt.value('blue')
    )
).add_selection(
    selector
)

enter image description here

Upvotes: 4

Related Questions