【问题标题】:how to create interactive slider in Julia in Makie如何在 Makie 的 Julia 中创建交互式滑块
【发布时间】:2022-11-11 15:10:19
【问题描述】:

我想创建一个带有滑块调节拓扑图时间的拓扑图。

在最小情况下 - 只需在滑块上打印值,在最大情况下 - 绘制当时的地形图。

我在 Makie 中找到了有关滑块的本教程。 https://www.youtube.com/watch?v=odpoatozNz8&ab_channel=doggodotjl 视频中的代码完美运行。 但它不适用于我的情节。 这是我的代码:

let 
    t = 100 # @lift($time)
eeg_topoplot(mean(dat_e[1:30, t, :], dims=2)[:,1], # averaging all trial of 30 participants on 100th msec
raw.ch_names[1:30]; 
positions=pos, # produced  automatically from ch_names
label_text=true,
axis=(aspect=DataAspect(),)) # aspect ratio, correlation of height and width

fig = current_figure()
xs = range(-30, 120, length = size(dat_e, 2))

lsgrid = labelslidergrid!(fig,
["time"],
Ref(LinRange(xs));
formats = [x -> "$(round(x, digits =0))"],
labelkw = Dict([(:textsize, 20)]),
sliderkw = Dict([(:linewidth, 20)]),
valuekw = Dict([(:textsize, 20)])
)

# set starting position for slope
set_close_to!(lsgrid.sliders[1], 120)

# layout sliders, put slider under the field

sl_sublayout = GridLayout(height = 80)
fig[2, 1] = sl_sublayout
fig[2, 1] = lsgrid.layout

# create listener
time = lsgrid.sliders[1].value
y = @lift($time .* 0 .+ $time)

#t = time
# add text
text!(0.5, -0.2,  text = "[" .* string.(y).* " ms]", align = (:center, :center))

hidedecorations!(current_axis())
hidespines!(current_axis()) 
fig

end

代码现在很糟糕,例如你可以问为什么你有 y = @lift($time .* 0 .+ $time)?那是因为这个问题,目前这不是我的目标。

LoadError: You can't @lift an expression that only consists of a single 
observable.

我目前的痛苦是这个问题:

MethodError: no method matching length(::Observable{Float64})
Closest candidates are:
length(!Matched::Union{Base.KeySet, Base.ValueIterator}) at abstractdict.jl:58
robin_dict.jl:86

这段代码适用于数组,我明白这一点。但是,如果我只想获取当前滑块上的一个值并将其作为文本或函数放入,我该怎么办?

【问题讨论】:

    标签: julia interactive makie.jl


    【解决方案1】:

    这个例子有帮助吗?

    T = 10
    
    pts = range(-1, 1, length=100)
    ts = reshape(1:T, 1, 1, :)
    topo = cos.(pts) .+ cos.(ts .* pts')
    
    fig = Figure()
    ax = Axis(fig[1, 1])
    
    sg = SliderGrid(fig[2,1],
        (label="time", range=1:10))
    
    time = sg.sliders[1].value
    
    str = lift(t -> "[$t ms]", time)
    text!(ax, str)
    
    topo_slice = lift((t, data) -> data[:, :, t], time, topo)
    contour!(ax, topo_slice)
    
    hidedecorations!(current_axis())
    hidespines!(current_axis()) 
    fig
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2022-12-13
      • 2016-11-23
      • 1970-01-01
      • 2022-08-23
      • 2020-06-06
      • 2020-05-06
      • 2020-03-12
      • 2012-07-16
      相关资源
      最近更新 更多