# Jupyter, plot, and last value?

**URL:** <https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386>\
**Category:** New to Julia\
**Tags:** question, jupyter, plotting\
**Created:** [December 20, 2021, 5:21pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386 "2021-12-20T17:21:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [December 20, 2021, 5:21pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/1 "2021-12-20T17:21:29Z")

</div>

It seems to me that to get a plot in a Jupyter notebook (or JupyterLab), a `plot` function call has to be the last value. Is that so?

```julia
using Plots
const xs = [-pi + i*(pi/10) for i in 0:20]
for j = 1:3
  ys = sin.(j*xs)
  plot!(xs,ys)
end
ys = cos.(xs)
#plot!(xs,ys) # <-- I need this to get a plot

```

I wonder what the standard solution is.

I’m just new to Jupyter and I’m using CoCalc, which says it uses Julia 1.6 .

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [December 20, 2021, 5:29pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/2 "2021-12-20T17:29:58Z")

</div>

Hey there and cool you are using Jupyter!  
In my experience, Julia cells executed in Jupyter is that there is an implicit return from the last executed function in the cell.  
If you want the plot displayed, I might suggest rather doing something like this:

```julia
using Plots
const xs = [-pi + i*(pi/10) for i in 0:20]
p = plot()
for j = 1:3
  ys = sin.(j*xs)
  plot!(xs,ys)
end
ys = cos.(xs)
display(p)

```

That should display the plot.  
I don’t have a notebook open right now so if it fails, I apologize!  
Let me know if this helps.

~ tcp 🌳

---

<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:** [December 20, 2021, 6:43pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/3 "2021-12-20T18:43:13Z")

</div>

`display` will do the trick, but needs an explicit plot object (which I think is good to have anyway to reduce confusion). A more direct alternative is to call `current()`

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [December 20, 2021, 6:43pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/4 "2021-12-20T18:43:36Z")

</div>

What @TheCedarPrince is correct. Conceptually, a `plot` (or `plot!`) call just _creates_ a Plot, just like `x = 5` creates an `Int`. When `x = 5` is at the end of a cell, the value `5` gets automatically displayed by Jupyter, and the same for a `plot` call - the returned `Plot` object gets displayed, which happens to be the image of the plot. If the `x = 5` assignment happened earlier in the cell, but we wanted the value of x to be shown when we run the cell, we could place an extra line at the end of the cell that just said `x` - and the same applies for the plot object `p`. (The explicit call to `display` in the TheCedarPrince’s code is not necessary - just having `p` as the last value of the cell will automatically have it be displayed - but doesn’t hurt either.)

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 20, 2021, 9:53pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/5 "2021-12-20T21:53:06Z")

</div>

I do it like this

```julia
plot()
for i=1:3
   plot!(<stuff>)
end
plot!()

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [December 21, 2021, 3:39am UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/6 "2021-12-21T03:39:19Z")

</div>

@TheCedarPrince  
That’s exactly what I was looking for! Thanks. I suppose

```julia
   plot!(p, xs, ys) # <-- explicitly use "p"

```

is what you meant, right?

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [December 21, 2021, 3:49am UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/7 "2021-12-21T03:49:25Z")

</div>

Thank you all for your input!

I wonder which document I should look at? All tutorials I’ve found on the Net show code like mine (without an explicit plot object).

I’m still new to Julia and indeed when I first saw the `plot` function, I was delighted that it’s so simple and yet versatile. At the same time, I was a bit surprised that it didn’t involve a plot object. (What if I wanted two separate plots?)

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 21, 2021, 12:02pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/8 "2021-12-21T12:02:00Z")

</div>

Julia’s online documentation is superb, IMHO. Both the core language and major packages. Check out [Combining Multiple Plots as Subplots](https://docs.juliaplots.org/latest/tutorial/#Combining-Multiple-Plots-as-Subplots) from the Julia Plots Tutorial.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 1:46pm UTC](https://discourse.julialang.org/t/jupyter-plot-and-last-value/73386/9 "2021-12-21T13:46:38Z")

</div>

> [@TheCedarPrince](#):
>
> `display(p)`

If this is the last line of the cell, you can just put

```julia
p

```

since the last-executed expression is automatically displayed as the cell output. That’s also why putting `current()` (with no `display`) works.
