PlotlyJS vs Plots+plotlyjs()

I like the Plots package together with plotlyjs backend for interactivity. But when compare to pure PlotlyJS package I see some drawbacks.

  • Figure from Plots has defined size and in PlotlyJS the size of the figure changes its size according the window size
  • Plots in heatmap loses the hover info and when zooming the ticks are not updated both is working in pure PlotlyJS package

Is there way how to fix it?

I use pure PlotlyJS instead of Plots. I’ve never have been able to get the PlotlyJS to resize properly. What platform are you on, and what did you do (in other words what sequence of functions and settings did you use) to get this to work?
Many thanks.
P

1 Like

I get the same behavior (resizing using PlotlyJS but not possible with Plots). So if anyone knows how to address that, that would be great.

@PetrKryslUCSD On my machine (macOS Catalina, Julia 1.5.1) the following code do the trick

julia> using PlotlyJS; # Using PlotlyJS
julia> plot(randn(100)) # ==> The plot changes its size accordingly
julia> using Plots
WARNING: using Plots.plot in module Main conflicts with an existing identifier.
julia> plotlyjs(); # Go to plotlyjs backend
julia> Plots.plot(randn(100)) # The plot does not change its size accordingly to its window

Linux and MacOS

I’m sorry to report that I’ve tried plotting with PlotlyJS both on Windows 10 and in WSL2 (Ubuntu 20.4)
in the hope that I will be able to gracefully resize the plot. It doesn’t work :frowning:

To explain what I was hoping to get (@sglyon will be able to tell me whether or not that is realistic), I wanted to see the figure to resize itself to fill the resized window. That doesn’t happen.

s

Does this work for any of you?

I got the resizing working to some extent while the program is running using

w = pl.window
ws = size(w)
relayout!(pl, Dict(:width=>ws[1], :height=>ws[2]))

Somehow it doesn’t seem the right way to go though.
Given that it only works while the window is actively controlled from Julia.

The correct solution is not to set the height and width, but to set autosize:

layout = Layout(;autosize = true, 
  xaxis = attr(title="x", range=[0.0, L]), 
  yaxis = attr(title = "F", range=[-1.0, 1.0]))

Then the figure is responsive during the simulation as well as after.
s

9 Likes

Many thanks. Wan you provide the complete code you use ? In a similar fashion, I tried to use @layout macro (as Layout is unknown with Plots, it is defined in Plotlyjs) but it does not work on my side.

I try this

    using Plots;
    plotlyjs();
    N = 1024;
    x = 0:N-1;
    y = sin.(2π*0.01 .*x);
    plot(x,y,@layout(autosize=true))

https://github.com/PetrKryslUCSD/Elfel.jl/blob/3e4721cd9cffb5bbb10aa8bc38eed0249503a373/examples/klein_gordon/klein_gordon_var.jl#L195

1 Like

I think I figured it out! Simply set the size to (NaN, NaN), for example

using Plots
plotlyjs()
N = 1024;
x = 0:N-1;
y = sin.(2π*0.01 .*x);
plot(x,y; size=(NaN, NaN))

Now the plot perfectly fits the window and auto-resizes.

2 Likes

You can also pass it directly to

plotlyjs(size=(NaN, NaN))

what about the second topic:

Plots in heatmap loses the hover info and when zooming the ticks are not updated both is working in pure PlotlyJS package

Nice proposal !
From my side, it doesn’t work though, I get an error related to truncation.

julia> plot(x,y)

Error showing value of type Plots.Plot{Plots.PlotlyJSBackend}:
ERROR: InexactError: trunc(Int64, NaN)
Stacktrace:
 [1] trunc at ./float.jl:703 [inlined]

with the following package versions

Status `~/.julia/environments/v1.5/Project.toml`
[717857b8] DSP v0.6.8
[31a5f54b] Debugger v0.6.6
[5789e2e9] FileIO v1.4.3
[d0351b0e] InspectDR v0.3.9
[47be7bcc] ORCA v0.5.0
[8314cec4] PGFPlotsX v1.2.10
[f0f68f2c] PlotlyJS v0.14.0
[91a5bcdd] Plots v1.6.10
[295af30f] Revise v3.1.5

It’s interesting that in the VS Code plot pane it works but from repl it does not.