# How to plot a subplots in PlotlyJS

**URL:** https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424
**Category:** Visualization
**Tags:** question, plotlyjs
**Created:** [November 23, 2019, 5:25pm UTC](https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424 "2019-11-23T17:25:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [November 23, 2019, 5:25pm UTC](https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424/1 "2019-11-23T17:25:24Z")

</div>

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

What I have:

```julia
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?

---

<div class="post-metadata">

### Author: ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)
#### Post date: [November 26, 2019, 4:10am UTC](https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424/2 "2019-11-26T04:10:48Z")

</div>

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:

```julia-auto
using InspectDR
include(joinpath(dirname(pathof(InspectDR)), "../sample/demo7.jl"))

```

The output you should get is the following:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e36f17dc1ea5b17139a8a879a0413c5a8d482e7.png)

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:

→ [GitHub - ma-laforge/InspectDR.jl: Fast, interactive Julia/GTK+ plots (+Smith charts +Gtk widget +Cairo-only images)](https://github.com/ma-laforge/InspectDR.jl)

### 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?

---

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [November 26, 2019, 11:58am UTC](https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424/3 "2019-11-26T11:58:42Z")

</div>

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.

---

<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: [November 26, 2019, 1:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424/4 "2019-11-26T13:37:29Z")

</div>

For a minimal example append to your code:

```julia
trace1[:yaxis]=trace2[:yaxis]="y2"
lyo=Layout(yaxis_domain=(0, 0.5), yaxis2_domain=(0.5, 1))
plot([trace1,trace2,trace3,trace4],lyo)

```

Pse see also [http://spencerlyon.com/PlotlyJS.jl/examples/subplots/](http://spencerlyon.com/PlotlyJS.jl/examples/subplots/)

> function subplots\_withsharedaxes()

---

<div class="post-metadata">

### Author: ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)
#### Post date: [November 27, 2019, 3:31am UTC](https://discourse.julialang.org/t/how-to-plot-a-subplots-in-plotlyjs/31424/6 "2019-11-27T03:31:05Z")

</div>

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