问题
I am using gnuplot 5.2.7 on Arch Linux. I want to temporarily change the terminal's configuration, plot something, and then restore it (I have no terminal configuration in my initialization file). I think pop
and push
can be used to this effect, but I'm having no success.
This is what I do in a gnuplot session. First I set the terminal to wxt
and push it, then plot a sine wave:
gnuplot> set term wxt 1 ; set term push
Terminal type is now 'wxt'
Options are '1 enhanced'
pushed terminal wxt 1 enhanced
gnuplot> plot sin(x)
So far this works. Now I want to temporarily change the background to cyan, and then revert to default background:
gnuplot> set term wxt 1 background "cyan"
Terminal type is now 'wxt'
Options are '1 background '#00ffff' enhanced'
gnuplot> plot sin(x)
gnuplot> set term pop
restored terminal is wxt 1 background '#00ffff' enhanced
gnuplot>
As you can see, pop
ing the terminal didn't restore the background. The next plot comes up with a cyan background.
Gnuplot's manual (pdf) states, in page 257, that:
The command
set term push
remembers the current terminal including its settings whileset term pop
restores it.
What am I doing wrong?
回答1:
From the gnuplot manual:
The command set term push remembers the current terminal including its settings while set term pop restores it. This is equivalent to save term and load term, but without accessing the filesystem. Therefore they can be used to achieve platform independent restoring of the terminal after printing, for instance. After gnuplot's startup, the default terminal or that from startup file is pushed automatically. Therefore portable scripts can rely that set term pop restores the default terminal on a given platform unless another terminal has been pushed explicitly.
Actually, it's not completely clear to me what is the benefit of terminal push
and terminal pop
? Well, restoring the default terminal. The only advantage I can (currently) think of is that in a long gnuplot script when you are switching back and forth to different terminals you don't have to type in all the parameters of your default terminal again and again. And in case you change some terminal settings you otherwise would have to change all the occurrences in your script.
Maybe the following is useful to you: at the beginning of the code define your terminals with your backgrounds or other settings as string variables and then later call them as macro with @
. So with this, I don't see a difference between calling @TerminalDefault
and set terminal pop
, except that @TerminalDefault
will also restore if you had the same terminal before but just with different settings.
Code:
### workaround for terminal push & pop with same terminal but different settings
reset session
TerminalDefault = 'set term wxt 0 background "white"'
TerminalCyan = 'set term wxt 0 background "cyan"'
TerminalYellow = 'set term wxt 0 background "yellow"'
TerminalPNG = 'set term png background "green"'
@TerminalDefault
plot x
pause -1 TerminalDefault
@TerminalCyan
plot x**2
pause -1 TerminalCyan
@TerminalPNG
set output "Test.png"
plot x**3
set output
pause -1 TerminalPNG
@TerminalDefault
plot x**4
pause -1 TerminalDefault
### end of code
来源:https://stackoverflow.com/questions/56499091/how-to-use-set-term-push-and-set-term-pop-in-gnuplot