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

**URL:** https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914
**Category:** Visualization
**Created:** [October 5, 2018, 6:27am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914 "2018-10-05T06:27:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Moses](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Moses](https://discourse.julialang.org/u/Moses)
#### Post date: [October 5, 2018, 6:27am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/1 "2018-10-05T06:27:09Z")

</div>

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

```julia
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.

```julia
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
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

```julia
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.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [October 5, 2018, 6:55am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/2 "2018-10-05T06:55:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Moses](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Moses](https://discourse.julialang.org/u/Moses)
#### Post date: [October 5, 2018, 7:39am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/3 "2018-10-05T07:39:57Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

### Author: ![natemcintosh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natemcintosh/32/9021_2.png) [@natemcintosh](https://discourse.julialang.org/u/natemcintosh)
#### Post date: [October 22, 2018, 8:20pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/4 "2018-10-22T20:20:18Z")

</div>

If you intend on using just PlotlyJS, you can do `using PlotlyJS` (with no `using Plots`) and then do something like [the docs](http://spencerlyon.com/PlotlyJS.jl/examples/line_scatter/) for PlotlyJS by itself.

```julia
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.

---

<div class="post-metadata">

### Author: ![Moses](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Moses](https://discourse.julialang.org/u/Moses)
#### Post date: [October 26, 2018, 10:28am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/5 "2018-10-26T10:28:39Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 15, 2020, 4:13am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/6 "2020-02-15T04:13:58Z")

</div>

Is there a solution?

---

<div class="post-metadata">

### Author: ![Igor\_Douven](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/igor_douven/32/8433_2.png) [@Igor\_Douven](https://discourse.julialang.org/u/Igor_Douven)
#### Post date: [February 15, 2020, 10:06am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-add-another-set-of-data-to-an-existing-plot/15914/7 "2020-02-15T10:06:35Z")

</div>

Yes there is. For instance, let

```julia
p1 = linescatter1()

```

Then one can add a trace as follows:

```julia
addtraces(p1, scatter(;x = 1:6, y = rand(5:15, 6), mode="markers+lines"))

```
