# Is PlotlyJS.add\_hline!() on y2-axis possible?

**URL:** <https://discourse.julialang.org/t/is-plotlyjs-add-hline-on-y2-axis-possible/100553>\
**Category:** General Usage\
**Tags:** plotlyjs\
**Created:** [June 19, 2023, 9:44am UTC](https://discourse.julialang.org/t/is-plotlyjs-add-hline-on-y2-axis-possible/100553 "2023-06-19T09:44:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [June 19, 2023, 9:44am UTC](https://discourse.julialang.org/t/is-plotlyjs-add-hline-on-y2-axis-possible/100553/1 "2023-06-19T09:44:30Z")

</div>

Is it possible under `PlotlyJS` to add hlines on “y#” (e.g. “y2”)?  
Here my sample code:

```julia
using PlotlyJS
# ---
function plot_debug_hline()
    scatter1 = PlotlyJS.scatter(;y = [1, 2, 3, 4], name= "Y1")
    scatter2 = PlotlyJS.scatter(;y = [+2.5, +1.5, 0.5, -1.5], name= "Y2", yaxis= "y2")
    plt_traces = [scatter1, scatter2]
    plt_layout = PlotlyJS.Layout(
        title_text = "Debug add_hline!()",
        xaxis_title_text = "xaxis title",
        yaxis_title_text = "yaxis title",
        yaxis2 = PlotlyJS.attr(
            title = "yaxis2 title",
            overlaying = "y",
            side = "right",
            ),            
        )
    # ---
    _fig_obj = PlotlyJS.Plot(plt_traces, plt_layout)
    PlotlyJS.add_hline!(_fig_obj, 2; line_color = "blue", line_dash = "dash", line_width = 1)
    PlotlyJS.add_hline!(_fig_obj, 1.5; line_color = "red", line_dash = "dash", line_width = 1, yaxis = "y2")
    # PlotlyJS.add_hline!(_fig_obj, 1,5; line_color = "green", line_dash = "dash", line_width = 1, yref = "y2") # "yref" does not work :-(
    # ---
    return _fig_obj
end
fig_obj = plot_debug_hline()

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [June 19, 2023, 2:37pm UTC](https://discourse.julialang.org/t/is-plotlyjs-add-hline-on-y2-axis-possible/100553/2 "2023-06-19T14:37:34Z")

</div>

As a workaround you might use the low level function: `PlotlyJS.hline()`.  
The only annoying thing is, I have not figured out how to retrieve the x-axis range:

```julia
using PlotlyJS
# ---
function plot_hline_on_axis_y2()
    _x_range = [1, 4]
    scatter1 = PlotlyJS.scatter(;y = [1, 2, 3, 4], name= "Y1")
    scatter2 = PlotlyJS.scatter(;y = [+2.5, +1.5, 0.5, -1.5], name= "Y2", yaxis= "y2")
    plt_traces = [scatter1, scatter2]
    plt_layout = PlotlyJS.Layout(
        title_text = "Add hline on axis y2!",
        xaxis_title_text = "xaxis title",
        xaxis_range = _x_range,
        yaxis_title_text = "yaxis title",
        yaxis2 = PlotlyJS.attr(
            title = "yaxis2 title",
            overlaying = "y",
            side = "right",
            ),            
        )
    # ---
    _fig_obj = PlotlyJS.Plot(plt_traces, plt_layout)
    PlotlyJS.relayout!(_fig_obj, shapes = PlotlyJS.hline([1.5], 0, _x_range; line_color = "red", line_dash = "dash", yref = "y2",) ,)
    PlotlyJS.add_hline!(_fig_obj, 2; line_color = "blue", line_dash = "dash", line_width = 1)
    # ---
    return _fig_obj
end
fig_obj = plot_hline_on_axis_y2()

```
