牵牛星中的对数滑块

2024-04-24 08:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

Altair提供具有等间距值的滑块。但是,滑块是否也可以映射到任意值?我特别感兴趣的是一个对数滑块,值为1, 10, 100(即左端1,中间10,滑块右端100)。p>

我正在寻找的一个例子可以在this JavaScript-related answer(它有一个很好的实时演示)中找到

这似乎是possible with matplotlib。(更新:这实际上不是一个好例子。请只看一下JavaScript例子。) 牵牛星也有可能吗


Tags: answermatplotlibwith对数javascriptthis感兴趣例子
1条回答
网友
1楼 · 发布于 2024-04-24 08:20:50

根据您想要对滑块执行的具体操作,可以使用计算变换来计算滑块值的指数。例如:

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

您可以以交互方式尝试here

相关问题 更多 >