PlotlyJS: How to add another set of data to an existing plot?


#1

I am trying to update an existing plot with a second trace using PlotlyJS with Julia 1.0 and Atom on Windows 8.1.

using Plots
using PlotlyJS

plotlyjs() # intended to switch to the according backend

x = [range(0, length = 100, stop = 2pi);]
y = sin.(x)
y2 = cos.(x)

If I next use plot(x, y) I get the error message that both Plots and PlotlyJS export this function and I need to specify the backend. It thought this was done by the plotlyjs() command, but obviously not.

PlotlyJS.plot(x, y)

displays the plot just fine in Atom’s plotting pane, with all the interactivity.

However, I cannot figure out how to add the y2-data to the plot.
plot!(x, y2) either uses the GR-backend or throws an error message within the Atom-GUI (not the REPL) (using the GR backend only seems to happen, if I had used it before, while the following error is thrown after freshly starting Atom):

Julia Client – Internal Error

Please call `using ORCA` to save figures
try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at event.jl:196
wait() at event.jl:255
wait(::Condition) at event.jl:46
wait(::Task) at task.jl:188
fetch at task.jl:202 [inlined]
macro expansion at dynamic.jl:67 [inlined]
(::getfield(Atom, Symbol("##113#119")))(::Dict{String,Any}) at eval.jl:86
handlemsg(::Dict{String,Any}, ::Dict{String,Any}) at comm.jl:164
(::getfield(Atom, Symbol("##19#21")){Array{Any,1}})() at task.jl:259

If I use PlotlyJS.plot!(x, y2) I get the error that plot! is not defined.

If I only rely on the Plots-Package (i.e. GR backend) then

plot(x, y)
plot!(x, y2)

works just fine.

I am not sure, whether this is a problem of

  1. my code
  2. the PlotlyJS package or
  3. the PlotlyJS/Plots integration.

Can somebody reproduce this problem and/or correct my code?
Thanks ahead.


#2

You should not call using PlotlyJS when using Plots. These are alternative interfaces.


#3

Thanks for the hint.
I adapted the code like this.

using Plots

plotlyjs() #to switch to the PlotlyJS backend

x = [range(0, length = 100, stop = 2pi);]
y = sin.(x)
y2 = cos.(x)

plot(x, y)
plot!(x, y2)

However, now the plot(x, y) command throws the “Julia Client - Internal Error” from my initial post.


#4

If you intend on using just PlotlyJS, you can do using PlotlyJS (with no using Plots) and then do something like the docs for PlotlyJS by itself.

function linescatter1()
    trace1 = scatter(;x = 1:4, y = [10, 15, 13, 17], mode="markers")
    trace2 = scatter(;x = 2:5, y = [16, 5, 11, 9],   mode="lines")
    trace3 = scatter(;x = 1:4, y = [12, 9, 15, 12],  mode="lines+markers")
    plot([trace1, trace2, trace3])
end
linescatter1()

This allows you to make as many traces as you want, and then put them all together at the end, with the plot([trace1, trace2, trace3]) statement.


#5

Yes, that works. However, it is not really flexible if I just want to add a trace to an existing plot (e.g. during testing).