我希望曲线的用户能够从 slider (斜率)调整曲线,但我希望斜率的值返回一组不同的值。
这样,如果用户选择 1,曲线将使用值 0.5,如果用户选择 5,曲线将使用值 0.8。
我尝试使用 lambda 表达式(如下面的脚本)和一些字典(因为我认为这是可行的方法),但我无法让它工作。
import numpy as np
import holoviews as hv
hv.extension('bokeh')
slope = [1, 5, 10]
def curve(slope):
x = np.linspace(1,10)
m = slope.apply(lambda x: 0.5 if slope == 1 else (0.8 if slope == 5 else (1)))
y = slope*0.5+x
err = x*m
return hv.Curve((x, y)) * hv.Spread((x,y,err))
curve_dict = {r:curve(r) for r in slope}
kdims = hv.Dimension(("slope", "slope"))
hv.HoloMap(curve_dict, kdims=kdims)
最佳答案
np.select()根据条件更改值:
slopes = [1, 5, 10]
def curve(slope):
xs = np.linspace(1,10)
slope = np.select(
condlist=[slope==1, slope==5],
choicelist=[0.5, 0.8],
default=1,
)
ys = slope*0.5+xs
return hv.Curve((xs, ys))
curve_dict = {slope: curve(slope) for slope in slopes}
hv.HoloMap(curve_dict, kdims='slope')
关于python - Holoviews 曲线中的关键维度用于在字典中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59829807/