# Collect trace indices meeting condition

**URL:** <https://discourse.julialang.org/t/collect-trace-indices-meeting-condition/75531>\
**Category:** Visualization\
**Tags:** plotlyjs\
**Created:** [January 31, 2022, 7:20pm UTC](https://discourse.julialang.org/t/collect-trace-indices-meeting-condition/75531 "2022-01-31T19:20:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)\
**Post date:** [January 31, 2022, 7:20pm UTC](https://discourse.julialang.org/t/collect-trace-indices-meeting-condition/75531/1 "2022-01-31T19:20:10Z")

</div>

Continuing the discussion from [Change each trace meeting condition in PlotyJS plot](https://discourse.julialang.org/t/change-each-trace-meeting-condition-in-plotyjs-plot/75474/2):

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:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [February 1, 2022, 10:22am UTC](https://discourse.julialang.org/t/collect-trace-indices-meeting-condition/75531/2 "2022-02-01T10:22:33Z")

</div>

A one-liner would be

```julia
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](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions).

---

<div class="post-metadata">

**Author:** ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)\
**Post date:** [February 2, 2022, 4:42am UTC](https://discourse.julialang.org/t/collect-trace-indices-meeting-condition/75531/3 "2022-02-02T04:42:06Z")

</div>

@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 👍

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.
