# Display Plots.jl plot in Juliabox

**URL:** <https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451>\
**Category:** Visualization\
**Created:** [November 20, 2016, 2:15pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451 "2016-11-20T14:15:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 20, 2016, 2:15pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/1 "2016-11-20T14:15:31Z")

</div>

I am using Plots.jl with PlotlyJS backend in Juliabox and I find myself in the situation of calling `add_plot` to add a trace at the time. I then call `gui()` at the end of the cell to display the plot. The following code used to work until a couple of weeks ago. It now works (in this minimal example) only if I remove `gui()`.  
In the code I have, it seems to work with `gui` only the first time the cell is executed, but not if I re-execute it. `pyplot` does not seem to show this problem, but pyplot doesn’t have remote interactivity.

```julia
using Plots
plotlyjs()

function add_plot()
    plot!(randn(10))
end

plot()
add_plot()
gui()

```

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [November 20, 2016, 2:31pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/2 "2016-11-20T14:31:22Z")

</div>

I actually have no idea how gui windows are handled in JuliaBox. I would  
expect this wouldn’t work… You should just return the plot object to the  
jupyter cell.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 20, 2016, 3:30pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/3 "2016-11-20T15:30:17Z")

</div>

As in having the plot object as the last line in the cell? What about if the call to `plot!` is in a function?

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [November 20, 2016, 3:54pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/4 "2016-11-20T15:54:27Z")

</div>

Both `plot` and `plot!` return a `Plot` object. Just make that object be  
the last thing in the cell.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 20, 2016, 10:43pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/5 "2016-11-20T22:43:46Z")

</div>

PyPlot used from Plots does not need this: I can update a plot deep from a function, without returning it and it has not problem displaying the plot. It seems only related to PlotlyJS,

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 20, 2016, 10:47pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/6 "2016-11-20T22:47:15Z")

</div>

what about using Plotly instead of PlotlyJS here?

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 21, 2016, 10:47am UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/7 "2016-11-21T10:47:53Z")

</div>

Same thing.  
But the fix is easy (although not convenient): return the plot as the last statement in the cell, just what Tom said.

```julia
function add_plot()
    plot!(randn(10))
end

p = plot()
add_plot()
p

```

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [November 21, 2016, 2:08pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/8 "2016-11-21T14:08:54Z")

</div>

For completeness… since `plot!` returns a `Plot` object, your (seemingly unnecessary but presumably as a minimal example) `add_plot` method will also return a `Plot` object. So `add_plot()` can be the last line of your cell.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [November 21, 2016, 2:54pm UTC](https://discourse.julialang.org/t/display-plots-jl-plot-in-juliabox/451/9 "2016-11-21T14:54:53Z")

</div>

You are right. In the code, the `add_plot` returns `nothing`.
