yogabonito
yogabonito

Reputation: 657

Logarithmic slider in Altair

Altair offers sliders with evenly spaced values. However, is it also possible to have arbitrary values the slider maps to? I'm especially interested in a logarithmic slider with values such as 1, 10, 100 (i.e. 1 on the left end, 10 in the middle, 100 on the right end of the slider).

An example of what I'm looking for can be found in this JavaScript-related answer (which has a nice live demo).

This seems to be possible with matplotlib. (UPDATE: This is actually not a good example. Please only have a look at the JavaScript example.) Is it also possible with Altair?

Upvotes: 1

Views: 222

Answers (1)

jakevdp
jakevdp

Reputation: 86328

Depending what exactly you want to do with the slider, you could use calculate transforms to calculate the exponent of the slider value. For example:

import altair as alt

slider = alt.binding_range(min=0, max=10, step=1, name='log(C)')
sel = alt.selection_single(name="sel", fields=['logC'],
                           bind=slider, init={'logC': 0})

alt.Chart(
    alt.sequence(0, 100, 1, 'x')
).transform_calculate(
    y=alt.datum.x ** 2 / alt.expr.exp(sel.logC)
).mark_line().encode(
    x='x:Q',
    y=alt.Y('y:Q', title='x^2/C'),
).add_selection(
    sel
)

enter image description here

You can try it interactively here.

Upvotes: 1

Related Questions