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.
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.
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.
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"]
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' "
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' "
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.
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.