How to plot a subplots in PlotlyJS

How can I use PlotlyJS.jl to make two subplots with two traces each with shared x-axis?

What I have:

using PlotlyJS

trace1 = PlotlyJS.scatter(x=1:3, y=rand(3))
trace2 = PlotlyJS.scatter(x=1:3, y=rand(3))
trace3 = PlotlyJS.scatter(x=1:3, y=rand(3))
trace4 = PlotlyJS.scatter(x=1:3, y=rand(3))

p1 = PlotlyJS.plot([trace1, trace2])
p2 = PlotlyJS.plot([trace3, trace4])
p = [p1; p2]

The layout is as I want but is there a way to have them to share the x-axis?

1 Like

If I run that code, both x-axes are the same - so Iā€™m guessing the problem is not that the x-axes are mismatched.

Tying x-axes together

If what you are looking for is to tie the x-axes together as you zoom in interactively, I think the only package that does that is InspectDR. If you want to try it out, you can just ]add InspectDR, then run the following code:

using InspectDR
include(joinpath(dirname(pathof(InspectDR)), "../sample/demo7.jl"))

The output you should get is the following:

Note that you can zoom in/out using the mousewheel or the +/- keys, etc. Check out the interactive mouse/keyboard bindings on the InspectDR.jl page:


Was that what you were looking to do?

If so, I suppose you could either:

  • Start using InspectDR.
  • Open a feature request on the PlotlyJS.jl repository to get the feature added. I am pretty certain it is not implemented (though I might be wrong).
  • Possibly contribute code to PlotlyJS.jl if you want to move things along?

Thank you for your answer.
I want to be able to zoom in one subplot and the other subplot should follow with its x-axis.
I want to use PlotlyJS because I need the html export.

For a minimal example append to your code:

lyo=Layout(yaxis_domain=(0, 0.5), yaxis2_domain=(0.5, 1))

Pse see also

function subplots_withsharedaxes()


I stand corrected. Very cool! I did not know Plotly supported this feature!