# The plot (with Plots pkg) not displayed, if plot(...) is not the last operator

**URL:** <https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [February 10, 2020, 12:58pm UTC](https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410 "2020-02-10T12:58:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [February 10, 2020, 12:58pm UTC](https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410/1 "2020-02-10T12:58:46Z")

</div>

This works as expected:

```julia
using Plots
gr()
x = 0:0.1:1
y = sin.(2π*x)
@show plot(x, y, lw=3)

```

and this does not display anything, no produce any error messages:

```julia
...
@show plot(x, y, lw=3)
a=1

```

```julia
and this works again (workaroung just found)
...
@show plot(x, y, lw=3)
a=1
plot!()

```

`a=1` is just an example: it is the same with any operator(s).  
`@show plot` added just to be sure the line was executed, and it returns in both cases:  
`julia> plot(x, y, lw=3) = Plot{Plots.GRBackend() n=1}`

Julia 1.3, Atom, Win10  
Julia 1.3.1, Atom, MacOS  
For the backend, I have tested with GR, PyPlot, PlotlyJS on Mac and GR on Win

Is that expected behavior? If yes, why?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 10, 2020, 2:29pm UTC](https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410/2 "2020-02-10T14:29:41Z")

</div>

Yes it is - this is not unique to plots, if you execute a block of code what’s shown is the value of the statement executed last (e.g. if you added `1+1` somewhere in your code block you’d never see the `2`, while if you add `1+1` as your last line the result gets printed).

If you want to show a plot within a line of code that you’re executing, you should call `display()` on it. Alternatively, you can assign your plot to a variable like `p = plot(rand(100))`, and then just add `p` as your last line instead of the `plot!()` call you currently have.

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [February 10, 2020, 6:37pm UTC](https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410/3 "2020-02-10T18:37:23Z")

</div>

@nilshg thank you for the explanation and for the suggestions.

Actualy my problem arose as I wanted for plot from a loop (in a function), and now I have found two more discussions on exactly that problem [How to plot in for loop (or map)](https://discourse.julialang.org/t/how-to-plot-in-for-loop-or-map/33881) and [Plotting from within a loop using GR? - #5 by baggepinnen](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/5) .

My prefered solution is to call `current()` or `plot!()` as the last code line.

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [February 13, 2020, 6:13pm UTC](https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410/4 "2020-02-13T18:13:11Z")

</div>

You can call `display` on your plot object to have it display to a screen.

---

<div class="post-metadata">

**Author:** ![dalarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalarev/32/9881_2.png) [@dalarev](https://discourse.julialang.org/u/dalarev)\
**Post date:** [October 20, 2020, 5:45pm UTC](https://discourse.julialang.org/t/the-plot-with-plots-pkg-not-displayed-if-plot-is-not-the-last-operator/34410/5 "2020-10-20T17:45:32Z")

</div>

`display` doesn’t work for me in Juno, maybe because I’m using `return` somewhere in the function, or because I have the plot pane disabled.
