Collect trace indices meeting condition

Continuing the discussion from Change each trace meeting condition in PlotyJS plot:

Now I understand that the second argument in restyle! can’t just be a single index, but also a vector of indices. So my question this time is, of there is a possibility to collect the indices of traces that meet a condition, e.g. with something like findall?

The code is the same as before:

using DataFrames
using PlotlyJS

df = DataFrame(
    x = 1:5,
    a = rand(5),
    b_calc = rand(5),
    c = rand(5),
    d_calc = rand(5)
)

fig = plot()
for tag in [:a, :b_calc, :c, :d_calc]
    add_trace!(fig, scatter(x=df.x, y=df[!, tag], name=tag))
end

for (k,it) in enumerate(fig.plot.data)
    if endswith(String(it[:name]), "_calc")
        restyle!(fig, k, line_dash="dash")
    end
end

A one-liner would be

restyle!(fig, findall(x -> endswith(String(x[:name]), "_calc"), fig.plot.data), line_dash="dash")

Above the first argument to findall is an anonymous function.

3 Likes

@stephancb, thank you again. That was what I was searching for.

It seems I hadn’t understood so far what the x was good for in the anonymous function. That was an eye-opener :+1:

And I already guessed it, but wanted to be sure with a real world example: The for loop approach is much faster than the findall approach.