# PlotlyJS subplots

**URL:** https://discourse.julialang.org/t/plotlyjs-subplots/48173
**Category:** General Usage
**Tags:** plotting
**Created:** [October 11, 2020, 6:13pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots/48173 "2020-10-11T18:13:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)
#### Post date: [October 11, 2020, 6:13pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots/48173/1 "2020-10-11T18:13:09Z")

</div>

Is there a way how to plot subplots containing more than one scatter plot?

Here is just example where I tried to put 3 scatter plots into the first subplot:

```julia
using PlotlyBase

trace1a = scatter(x=1:3, y=2:4)
trace1b = scatter(x=1:3, y=3:7)
trace1c = scatter(x=1:3, y=10:20)
trace1 = [trace1a , trace1b, trace1c]
trace2 = scatter(x=20:10:40, y=[5,5,5], xaxis="x2", yaxis="y")
trace3 = scatter(x=2:4, y=200:100:800, xaxis="x", yaxis="y3")
trace4 = scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4")
data = [trace1, trace2, trace3, trace4]
layout = Layout(
    grid=attr(
        rows=2, columns=2, subplots=[["xy", "x2y"], ["xy3", "x4y4"]],
        roworder="bottom to top"
    )
)
Plot(data, layout)

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [October 11, 2020, 8:20pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots/48173/2 "2020-10-11T20:20:20Z")

</div>

Have you tried this?  
[http://juliaplots.org/PlotlyJS.jl/stable/examples/subplots/](http://juliaplots.org/PlotlyJS.jl/stable/examples/subplots/)

---

<div class="post-metadata">

### Author: ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)
#### Post date: [October 12, 2020, 6:31am UTC](https://discourse.julialang.org/t/plotlyjs-subplots/48173/3 "2020-10-12T06:31:00Z")

</div>

Thank you, I somehow missed it.

---

<div class="post-metadata">

### Author: ![sglyon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sglyon/32/67_2.png) [@sglyon](https://discourse.julialang.org/u/sglyon)
#### Post date: [October 12, 2020, 2:05pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots/48173/4 "2020-10-12T14:05:03Z")

</div>

Thanks @PetrKryslUCSD

Hi @Oto_Brzobohaty, you can use the built in plotly.js subplot tools

I think the problem was that you need to splat the `trace1` when creating the data array (by doing `trace1...`). As writetn in the original post, `data` will be a 4 element array, where the first element is itself an array of traces. What we want here is an array of 6 traces.

Here’s a working example (note that I also removed the `roworder="bottom to top"` in the layout because that was causing the bottom row to be filled in first, so `xy` was bottom left instead of top left):

```julia
using PlotlyBase

trace1a = scatter(x=1:3, y=2:4)
trace1b = scatter(x=1:3, y=3:7)
trace1c = scatter(x=1:3, y=10:20)
trace1 = [trace1a , trace1b, trace1c]
trace2 = scatter(x=20:10:40, y=[5,5,5], xaxis="x2", yaxis="y")
trace3 = scatter(x=2:4, y=200:100:800, xaxis="x", yaxis="y3")
trace4 = scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4")
data = [trace1..., trace2, trace3, trace4]

layout = Layout(
    grid=attr(
        rows=2, columns=2, subplots=[["xy", "x2y"], ["xy3", "x4y4"]],
# roworder="bottom to top"
    )
)

Plot(data, layout)

```

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [March 11, 2022, 8:52pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots/48173/5 "2022-03-11T20:52:35Z")

</div>

Sorry for reviving this old thread. I rewrote the code in the post above like this

```julia
trace1a = scatter(x=1:3, y=2:4, xaxis="x", yaxis="y")
trace1b = scatter(x=1:3, y=3:7, xaxis="x", yaxis="y")
trace1c = scatter(x=1:3, y=10:20, xaxis="x", yaxis="y")
trace2 = scatter(x=20:10:40, y=[5,5,5], xaxis="x2", yaxis="y")
trace3 = scatter(x=2:4, y=200:100:800, xaxis="x", yaxis="y3")
trace4 = scatter(x=4:6, y=7000:1000:9000, xaxis="x4", yaxis="y4")

plot([trace1a, trace1b, trace1c, trace2, trace3, trace4],
     Layout(grid=attr(rows=2, columns=2,
                      subplots=[["xy", "x2y"], ["xy3", "x4y4"]])))

```

However, if I change the definition for `trace4` and the grid attribute in the following way

```julia
trace4 = scatter(x=4:6, y=7000:1000:9000, xaxis="x", yaxis="y4") #xaxis="x" instead of "x4"

plot([trace1a, trace1b, trace1c, trace2, trace3, trace4],
     Layout(grid=attr(rows=2, columns=2,
                      subplots=[["xy", "x2y"], ["xy3", "xy4"]]))) # "xy4" instead of "x4y4"

```

the plot is messed up:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/4/b4861af72808112d537d529bc6d6e5c9e22fc7ee.png)
