[英]How do I select a line by clicking in Makie?
I want to be able to select a line on the plot by clicking on it.我希望能够通过单击 select 在 plot 上添加一行。 Ideally, when I click on any line, the number of that line will appear on the screen.
理想情况下,当我单击任何一行时,该行的编号将出现在屏幕上。 I wrote my code based on the tutorial, but there is no example with lines, so I did what I did.
我根据教程编写了我的代码,但是没有带行的示例,所以我做了我所做的。 https://docs.makie.org/v0.19/documentation/events/index.html#point_picking
https://docs.makie.org/v0.19/documentation/events/index.html#point_picking
At the moment I have no idea what these numbers are telling me and why.目前我不知道这些数字告诉我什么以及为什么。 They are not even coordinates of clicked points.
它们甚至不是点击点的坐标。
PS Actually it is just a starting point. PS 其实这只是一个起点。 I want to create event interaction on series and topoplots.
我想在系列和 topoplots 上创建事件交互。 But for now it would be great to find out the basics.
但就目前而言,找出基础知识会很棒。
f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
ax = Axis(f[1, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]")
N = 1:length(pos)
positions = Observable(rand(Point2f, 10))
xs = 0:0.01:10
ys = 0.5 .* sin.(xs)
lines!(xs, ys)
lines!(xs, ys * 2)
hidedecorations!(ax, label = false, ticks = false, ticklabels = false)
hidespines!(ax, :t, :r)
hlines!(0, color = :gray, linewidth = 1)
vlines!(0, color = :gray, linewidth = 1)
i = Observable(0)
on(events(f).mousebutton, priority = 2) do event
if event.button == Mouse.left && event.action == Mouse.press
plt, i[] = pick(f)
str = lift(i -> "$(i)", i)
text!(ax, 1, -0.5, text = str, align = (:center, :center))
end
end
f
Below are some examples of the interaction between clicking and the number displayed (the red dot is where I click).下面是点击和显示的数字之间交互的一些示例(红点是我点击的地方)。
Check out the mouseposition variable here:在此处查看 mouseposition 变量:
https://docs.makie.org/stable/api/#Events https://docs.makie.org/stable/api/#Events
or the registerInteraction: function here:或者 registerInteraction: function 在这里:
https://docs.makie.org/v0.19/examples/blocks/axis/index.html#registering_and_deregistering_interactions https://docs.makie.org/v0.19/examples/blocks/axis/index.html#registering_and_deregistering_interactions
You can use them both as below:您可以按以下方式使用它们:
using GLMakie
f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
ax = Axis(f[1, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]")
#N = 1:length(pos)
positions = Observable(rand(Point2f, 10))
xs = 0:0.01:10
ys = 0.5 .* sin.(xs)
lines!(xs, ys)
lines!(xs, ys * 2)
hidedecorations!(ax, label = false, ticks = false, ticklabels = false)
hidespines!(ax, :t, :r)
hlines!(0, color = :gray, linewidth = 1)
vlines!(0, color = :gray, linewidth = 1)
register_interaction!(ax, :my_interaction) do event, axis
if event.type === MouseEventTypes.leftclick
println("Graph axis position: $(event.data)")
end
end
i = Observable(0)
on(events(f).mousebutton, priority = 2) do event
if event.button == Mouse.left && event.action == Mouse.press
plt, i[] = pick(f)
str = lift(i -> "$(i)", i)
text!(ax, 1, -0.5, text = str, align = (:center, :center))
@show mouseposition(f)
end
end
f
Note that for some reason (perhaps it sees the first click as a selection?) Makie does not start registering the interaction on the graph until the first click within the graph, unlike the clicks on the figure which are all shown even the first one.请注意,出于某种原因(也许它将第一次点击视为选择?)Makie 直到在图表中第一次点击后才开始在图表上注册交互,这与图表上的点击不同,即使是第一次点击也会显示出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.