PlutoPlotly and PlotlyJS: subplots

Hi,
I am trying to produce a figure with two subplots in Pluto, using PlotlyJS and PlutoPlotly. If I create each plot individually, there is no problem: all the layout attributes of the individual plots will come out correctly. However, when I produce a figure with the two subplots, some attributes come out OK (like titles, hover, names), but not shapes. Strangely, shapes are supposed to appear in both subplots, but they are only visible in the first subplot. Unfortunately, I do need the shapes to appear in all subplots.

As usual, @empet solved my problem here:

using PlotlyJS
fig= make_subplots(rows=1, cols=2, subplot_titles=["Some noise" "More noise"])
add_trace!(fig, scatter(;x=1:50, y=rand(50), name = "some"),  row=1, col=1)
add_trace!(fig, scatter(;x=1:50, y=rand(50), name = "more"), row=1, col=2)
relayout!(fig, hovermode="x")
add_vrect!(fig,  5, 10,  fillcolor = "red", opacity = 0.2, line_width = 0; row=1, col=1)
add_vrect!(fig,  20, 26,  fillcolor = "blue", opacity = 0.2, line_width = 0; row=1, col=2)
display(fig)

If I run @empet’s code in Atom/VScode, having the internal plot pane activated, the figure with subplots comes out OK, and no error is thrown out.

However, suppose the external plot window is activated. In that case, the figure is still produced, but an infinite set of garbage pops out – as soon as we click on the figure – alleging problems with WebSockets, Mux, Blink, and WebIO.

The same code also works very well in VSCode/Atom using PlotlyJS, ElectronDisplay as proposed by @stephancb here

Nevertheless, I need these figures in Pluto, not Atom or VScode. I tried to replicate the same code in Pluto (v0.19.12), using PlotlyJS (v018.8) and PlutoPlotly (v.03.4). Still faced two problems: (i) without using WebIO, no plot is produced, and the infamous error WebIO not detected pops up.; (ii) with using WebIO, no error is thrown out, but the figure is nowhere to be seen. In the latter case, if I close the block by using display(fig), this is what I get:

and if I opt for closing the block by calling fig , nothing comes out.

I get the same behavior by using PlutoPlotly.jl or @disberd’s initial workaround to integrate PlotlyJS inside Pluto here.

Help will be very much appreciated. Thanks

Hi @VivMendes the problem here is that the function make_subplots is defined inside PlotlyJS and not inside PlotlyBase and it automatically create a SyncPlot (also from PlotlyJS) rather than a normal Plot object.

the function is quite simple, so for the time being you can just plug in this code to make it work:

begin
	fig= Plot(Layout(Subplots(rows=1, cols=2, subplot_titles=["Some noise" "More noise"])))
	add_trace!(fig, scatter(;x=1:50, y=rand(50), name = "some"),  row=1, col=1)
	add_trace!(fig, scatter(;x=1:50, y=rand(50), name = "more"), row=1, col=2)
	relayout!(fig, hovermode="x")
	add_vrect!(fig,  5, 10,  fillcolor = "red", opacity = 0.2, line_width = 0; row=1, col=1)
	add_vrect!(fig,  20, 26,  fillcolor = "blue", opacity = 0.2, line_width = 0; row=1, col=2)
	PlutoPlotly.plot(fig) # This need to be the `plot` function from PlutoPlotly, not from PlotlyJS
end

Now that I am back from holidays I have to work on some backlog stuff on PlutoPlotly to fix some things and make it more compliant with standard PlotlyJS usage, so hopefully within the next week or so you could still use the same code you use in PlotlyJS outside of Pluto without modifications.
Remember though that PlutoPlotly and PlotlyJS are not supposed to be imported together (at least not both with using) in a single notebook.
I will also try to adapt PlutoPlotly to be compliant with the recent JuliaPlots/PlotlyKaleido.jl (github.com) so that you don’t have to load PlotlyJS to use savefig anymore.

3 Likes

Hi @disberd, thank you so much. Your solution works nicely. It is a pleasure to work with Pluto and PlotlyJS, and you made it possible and efficient with your PlutoPlotly package. It works much faster than producing the plots in VSCode or Atom.