imantha
imantha

Reputation: 3828

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?

Upvotes: 3

Views: 1243

Answers (2)

petem
petem

Reputation: 810

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)

Upvotes: 3

Oskar
Oskar

Reputation: 1450

I would suggest you to use Plots.jl as it is a visualization interface and toolset that covers all the possible backends (e.g. GR, Plotly, PyPlot) and you can switch backend using always the same code with only minor changes. Plots also has a great documentation, with good instructions and examples on how to plot stuff.

Said this, I would implement your example as

using Plots
plotly() # or plotlyjs()
p1 = plot(collect(1:10), randn(10), line=(:coral, 3), label="coral line")
p2 = plot(collect(1:10), randn(10), line=(:thistle, 3), label="thistle line")
plot(p1, p2, layout=(2,1))

For more supported attributes you can have a look at the documentation: docs.juliaplots.org/latest/attributes/.

To the difference between plotly and plotlyjs you can read the docs.juliaplots.org/latest/backends/ page. plotly comes without the need of any extra dependency and you can work with it like that, plotlyjs on the other side is, as they put it, the preferred one, since it has more functionalities, you can save to more formats and it taps into Blink.jl and Electron to plot within a standalone GUI window.

Upvotes: 1

Related Questions