Gnuplot: how to plot into an existing figure?

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. :frowning:

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

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.

1 Like

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.

that’s exactly what that example is demoing right?

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

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.

1 Like
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

I am on Windows 10. Gnuplot 5.4.

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:

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 .

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

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

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' " 
1 Like

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):

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.

1 Like

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.

1 Like

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

1 Like