# Gnuplot: how to plot into an existing figure?

**URL:** <https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529>\
**Category:** General Usage\
**Tags:** plotting, gnuplot, gaston\
**Created:** [October 17, 2020, 3:07am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529 "2020-10-17T03:07:06Z")\
**Posts on this page:** 13\
**Page:** 1

<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:** [October 17, 2020, 3:07am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/1 "2020-10-17T03:07:06Z")

</div>

For the Gnuplot aficionados here: Say I plot once into a terminal. Then I call another function and I would like that plot to be added to that existing terminal. Can I do that? I searched through two Gnuplot books, without luck. ☹

I use the Gnuplot.jl package, in case you had doubts about the relevance of my question to this forum. 🙂

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 17, 2020, 3:10am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/2 "2020-10-17T03:10:51Z")

</div>

[https://gcalderone.github.io/Gnuplot.jl/v1.3.0/basic/#Multiple-datasets,-logarithmic-axis,-labels-and-colors,-etc.-1](https://gcalderone.github.io/Gnuplot.jl/v1.3.0/basic/#Multiple-datasets,-logarithmic-axis,-labels-and-colors,-etc.-1)

Is this the example you’re looking for? looks like you just need `:-`? or maybe I’m not understanding what you’re trying to achieve.

---

<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:** [October 17, 2020, 4:18am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/3 "2020-10-17T04:18:17Z")

</div>

OK: I plot into a terminal. Then the function returns. Now, let us say that some time later I would like add another curve to this plot. How do I do it? I didn’t do ‘multiplot’ to begin with. Can I do it later? I can’t tell.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 17, 2020, 4:26am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/4 "2020-10-17T04:26:22Z")

</div>

that’s exactly what that example is demoing right?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e691195d9766fceee6f3a35887823852ba9e177.png)

---

<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:** [October 17, 2020, 4:16pm UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/5 "2020-10-17T16:16:45Z")

</div>

OK, here is a bit of code that demonstrates what I’d like to do:

```julia
using Gnuplot

# This plotting is inside a function

function f()
	x = collect(-1:0.1:1)

	@gp "set terminal windows 0 " :-
	@gp :- x x.^2 "lw 2 lc rgb 'red' with lines title 'quadratic' " 

	@gp "set terminal windows 1 " :-
	@gp :- x x.^3 "lw 2 lc rgb 'red' with lines title 'cubic' " 
end

f()

# Now add another plot to terminal 0

@gp "set terminal windows 0 " :-
@gp :- x x.^4 "lw 2 lc rgb 'blue' with lines title 'quartic' " 

```

The problem is that this latest plotting command replaced the previous plot, it wasn’t added to it.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 17, 2020, 4:26pm UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/6 "2020-10-17T16:26:15Z")

</div>

```julia
julia> f()
┌ Error:
│
│ gnuplot> set terminal windows 0
│ ^
│ line 0: unknown or ambiguous terminal type; type just 'set terminal' for a list
└ @ Gnuplot ~/.julia/packages/Gnuplot/7y0ez/src/Gnuplot.jl:897
ERROR: Gnuplot error: ["unknown or ambiguous terminal type; type just 'set terminal' for a list"]

```

doesn’t work for me somehow

---

<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:** [October 17, 2020, 4:36pm UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/7 "2020-10-17T16:36:34Z")

</div>

I am on Windows 10. Gnuplot 5.4.

---

<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:** [October 17, 2020, 11:01pm UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/8 "2020-10-17T23:01:13Z")

</div>

Tested in Julia 1.5.2 the code below on Windows 10 (with Gnuplot 5.4) and last plot command does add curve to existing figure:

```julia
using Gnuplot

# This plotting is inside a function
function f()
	x = collect(-1:0.1:1)
	@gp :- x x.^2 "lw 2 lc rgb 'red' with lines title 'quadratic' " 
	@gp :- x x.^3 "lw 2 lc rgb 'green' with lines title 'cubic' " 
end

f()

x = collect(-1:0.1:1);
@gp :- x x.^4 "lw 2 lc rgb 'blue' with lines title 'quartic' " 

```

![gnuplot_overlay](https://global.discourse-cdn.com/julialang/original/3X/7/3/737c51c838ca12e5a275caa390450877b85a5850.png) .

---

<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:** [October 17, 2020, 11:11pm UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/9 "2020-10-17T23:11:05Z")

</div>

Right, but I have _two_ terminals. I want to select to which the plot goes.

---

<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:** [October 17, 2020, 11:45pm UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/10 "2020-10-17T23:45:05Z")

</div>

Ok and sorry for the misunderstanding. Could you check if code below is what you need?

```julia
using Gnuplot

# This plotting is inside a function outputs to two windows:
function f()
	x = collect(-1:0.1:1)
	@gp :GP1
	@gp :- :GP1 x x.^2 "lw 2 lc rgb 'red' with lines title 'quadratic' " 
	@gp :GP2
	@gp :- :GP2 x x.^3 "lw 2 lc rgb 'green' with lines title 'cubic' " 
end

f()

x = collect(-1:0.1:1);
@gp :- :GP1 x x.^4 "lw 2 lc rgb 'blue' with lines title 'quartic on top of 1' " 
@gp :- :GP2 x x.^4 "lw 2 lc rgb 'blue' with lines title 'quartic on top of 2' " 

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 18, 2020, 12:36am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/11 "2020-10-18T00:36:41Z")

</div>

With apologies for being sightly offtopic, here’s how to do it with Gaston, which is also based on gnuplot. Each plot is identified by a handle, which all `plot` commands take as an optional argument (default is most recent plot):

```julia
function f()
    x = collect(-1:0.1:1)
    plot(x, x.^2, w = "l lw 2 lc 'red' t 'quadratic'", handle = 1)
    plot(x, x.^3, w = "l lw 2 lc 'green' t 'cubic'", handle = 2)
end

f()

plot!(x, x.^4, w = "l lw 2 lc 'blue' t 'quartic on top of 1'", handle = 1)
plot!(x, x.^4, w = "l lw 2 lc 'blue' t 'quartic on top of 2'", handle = 2)

```

Another difference between Gnuplot.jl and Gaston is that, in this example, Gnuplot.jl will start two gnuplot processes, whereas Gaston relies on a single process.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 18, 2020, 12:46am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/12 "2020-10-18T00:46:35Z")

</div>

> [@PetrKryslUCSD](#):
>
> Can I do that? I searched through two Gnuplot books, without luck.

Petr, in gnuplot, you would need to execute `plot` or `splot` again, with all previous arguments and data, plus the new function you want to add. That is what Gaston’s `plot!` does under the hood.

---

<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:** [October 18, 2020, 1:30am UTC](https://discourse.julialang.org/t/gnuplot-how-to-plot-into-an-existing-figure/48529/13 "2020-10-18T01:30:45Z")

</div>

Thank you all. Gaston.jl is great, and so is Gnuplot.jl. So now I know how to do that with both packages. Bliss! 😃
