How to simulate `for_each_yaxis` (from Python) in Julia?

The title says it all (again). Currently I state all the axes manually to change their styles like in

p = make_subplots(
    rows=2,
    cols=1,
    shared_xaxes=true,
    
);

add_trace!(p, scatter(y=1:2), row=1, col=1)
add_trace!(p, scatter(y=1:2), row=2, col=1)

relayout!(
    p,
    xaxis=attr(showlines=true, mirror=true, linewidth=2),
    yaxis=attr(showlines=true, mirror=true, linewidth=2, linecolor="red"),
    yaxis2=attr(showlines=true, mirror=true, linewidth=2, linecolor="red"),
)

In Python you could do this for the y-axes with

p.for_each_yaxis(
    lambda axis: axis.update(
        showline=True, mirror=True, linewidth=2, linecolor="red"
    )

Is there a way to so something similar in Julia?

Here is one way:

using PlotlyJS

p = make_subplots(
    rows=2,
    cols=1,
    shared_xaxes=true,
    
);

add_trace!(p, scatter(y=1:2), row=1, col=1)
add_trace!(p, scatter(y=1:2), row=2, col=1)

## Gets y axes as fall back, or use "x" etc. in second argument to get other ones:
get_axes(q::PlotlyJS.SyncPlot, dm::String="y") = filter(z -> occursin(dm*"axis",string(z)), keys(q.plot.layout))

# Define your updates to axis properties here:
update_dict = Dict(:showlines => true, :mirror => true, :linewidth => 2, :linecolor => "red")

for ax in get_axes(p,"y")
    merge!(p.plot.layout[ax], update_dict)
end

relayout!(
    p,
    xaxis=attr(showlines=true, mirror=true, linewidth=2)
)
display(p)
1 Like