Search code examples
juliaplotly

Making Subplots in Julia Plotly.jl


Since the documentation available on the net for Plotly.jl is quite ancient, it seems that the syntax has changed. Now I am wondering how to make subplots in Plotly v0.3.0 For example, if I had two traces such as this, how do I put them in two horizontal subplots (rows = 2, cols = 1)

 using Plotly 

 trace1 = scatter(
     x = collect(1:10),
     y = randn(10),
     mode = lines
     line = Dict(
         :color => "coral"
         :width => 3
     )
     name = "coral line"
)

trace2 = scatter(
     x = collect(1:10),
     y = randn(10),
     mode = lines
     line = Dict(
         :color => "thistle"
         :width => 3
     )
     name = "thistle line"
)

#To add them to the same plot
data = [trace1, trace2]
Plotly.plot(data)

But how would I add them into two different subplots in Julia Plotly? For example in python you would use the fig = make_subplots(rows =2 , cols = 2) functions and specify the row and column in each trace fig.add_trace(go.Scatter(...), row = 2, col = 1). ANy idea how to do something similar in Julia

Also on a side not any idea whats the difference between Plotly and PlotlyJS in Julia?


Solution

  • Plotly.jl is now obsolete. Use it only for sending plots to Plotly cloud. The last version of PlotlyJS.jl (v0.18.8) is recommended. For examples see https://plotly.com/julia/

    Code for your subplots:

    using PlotlyJS
    
    fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.065) 
    
     trace1 = scatter(
         x = collect(1:10),
         y = randn(10),
         mode = "lines",
         line = attr(
                    color="coral",  
                    width = 3),
         #or line_color ="coral", line_width=3
         name = "coral line")
    
    trace2 = scatter(
         x = collect(1:10),
         y = randn(10),
         mode = "lines",
         line = attr(
             color ="thistle",
             width = 3,
         ),
         name = "thistle line")
    add_trace!(fig, trace1, row=1, col=1)
    add_trace!(fig, trace2, row=1, col=2)
    #update the default layout created by make_subplots
    relayout!(fig, title_text="PlotlyJS subplots", title_x=0.5, 
              width=700, height=300)
    display(fig)