# Strange behavior of plot---plot successful only if it is the last line of the code

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [January 18, 2024, 11:52am UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952 "2024-01-18T11:52:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 18, 2024, 11:52am UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952/1 "2024-01-18T11:52:01Z")

</div>

I am using vscode in anaconda. The following code

```julia
using Plots

a = 3+4
plot([1,2,3],[2,4,8])

```

can produce a figure. But this code cannot

```julia
using Plots

plot([1,2,3],[2,4,8])
a = 3+4

```

It seems that the plot is displayed only if it is the last line of the code.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 18, 2024, 11:57am UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952/2 "2024-01-18T11:57:58Z")

</div>

```julia
using Plots

p=plot([1,2,3],[2,4,8])
a = 3+4
p

```

---

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 18, 2024, 12:05pm UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952/3 "2024-01-18T12:05:35Z")

</div>

It works!

But what if I have more than two figures to plot?

```julia
using Plots

p=plot([1,2,3],[2,4,8])
p2=plot([1,2,3],[2,4,-8])
a = 3+4

p
p2

```

Only the last figure is plotted.

---

<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:** [January 18, 2024, 1:06pm UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952/4 "2024-01-18T13:06:48Z")

</div>

> [@Make plots in sepeare windows](https://discourse.julialang.org/t/make-plots-in-sepeare-windows/45815):
>
> I want to plot different arrays and show the plot in separate windows when I run the julia script from the REPL using the include("\<name\>.jl") I searched it on the internet and I got to know about reuse = false, but when I run this: p1 = plot([1,2,3,4,5],[1,2,3,4,5]) #Linear display(p1) p2 = plot([1,2,3,4,5],[1,4,9,16,25],resue = false) #quadratic display(p2) I get only one plot of the quadratic. I want to see the linear in a different window and the quadratic window in a separate window…

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [January 18, 2024, 3:07pm UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952/5 "2024-01-18T15:07:21Z")

</div>

When you input something to the REPL (including a file), the result of the last line is shown. It is shown via the `display` function. So

```julia
using Plots

display(plot([1,2,3],[2,4,8]))
plot([1,2,3],[2,4,8]) |> display # same as above, but I think |> looks nicer here
a = 3+4

```

will display the plot twice and then display 7 since it’s the final line.

(Unless, of course, the second plot overwrites the first. Whether that is possible depends on the specifics of the plotting package you use and doesn’t seem to be a problem that you’ve indicated so far.)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2024, 3:17pm UTC](https://discourse.julialang.org/t/strange-behavior-of-plot-plot-successful-only-if-it-is-the-last-line-of-the-code/108952/6 "2024-01-18T15:17:41Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> But what if I have more than two figures to plot?

With Plots.jl you have two options: (i) `plot(p, p2)` , (ii) create two separate windows.  
The latter works for a number of backends - [see this thread](https://discourse.julialang.org/t/how-to-display-multiple-plot-windows).
