Show graph on display and save it to file simultaneously in gnuplot - gnuplot

How can I save graph in to file and also print it to display? I've tried:
#!/usr/bin/gnuplot -p
date=system("date +%F_%T | sed 's/:/-/g'")
set term png
set output date.".png"
set term x11
set out
plot sin(x)
PS: Is there a possibility to save the graph which is displayed in gnuplot window? I've noticed that there is copy to clipboard button but no save.

If you want to send a plot both to a file and to an interactive terminal like x11 or wxt you have the replot after you changed the terminal
set terminal png
set output 'file.png'
plot sin(x)
set terminal x11
set output
replot
If you don't want to set the x11 terminal explicitely, but rather use the default terminal, whatever it is, you can use the special terminals push and pop so save and restore a terminal:
set terminal push
set terminal pngcairo
set output 'file.png'
plot sin(x)
set terminal pop
set output
replot
To make this more transparent and save any image after you plotted it to an interactive terminal you could define a gnuplot script export.gp which you can then call and give the output file name as parameter.
The export.gp script is
set terminal push
set terminal pngcairo
set output '$0'
replot
set output
set terminal pop
which you can then use as
plot sin(x)
call 'export.gp' 'test.png'
Note, however, that the exported file and the plot shown in the interactive window will be different, but if you use wxt as interactive and pngcairo or pdfcairo as output terminals, the chances are quite high, that displayed and exported images are very similar.
With gnuplot 5.0 the qt and wxt terminals offer an "Export" button to save exactly the image shown in the window as svg, pdf or png files. Unfortunately, this functionality cannot yet be invoked from a script, i.e. there is no export command.

A good answer was also given in gnuplot - How can I save a graphics file of a plot that is the same as I designed it in xterminal?.
For x11 terminal one can use
system("xwd -id ".GPVAL_TERM_WINDOWID." | convert xwd:- screenshot.png")
It can be also wrapped into a short cut
bind "Ctrl-c" 'system("xwd -id ".GPVAL_TERM_WINDOWID." | convert xwd:- png:- | xclip -sel clip -t image/png")'
So you plot the image
set term x11
plot sin(x)/x
and then press Ctrl+c in the plot window. Just here, I pasted the image with Ctrl+v:
Unfortunately, it does not work for qt or wxt (GPVAL_TERM_WINDOWID is associated to x11). They have clipboard buttons, but snapshots are not scriptable.

Just expanding on Chistofs comprehensive answer
I like to keep the save commands and decide the file name at the end.
So, modifying the answer gave me this
plot sin(x)
set terminal push
set terminal pngcairo
set output 'test.png'
replot
set output
set terminal pop

Related

Inkscape crops eps files generated with gnuplot

I have a script file to generate an eps file with gnuplot. The basics of this script are:
set terminal postcript enhanced color size 30,20 font 'Times-ew-Roman,40'
set xtics -.5,0.125
set ytics 0.1,0.1
set xrange [-0.5,.5]
set yrange [0.,1.6]
set cbrange [-0.5,.5]
set output "file.eps"
plot #whatever i plot
This script generates an eps file, which I can open in ubuntu and I can see is well printed. Now, I want to import this eps into inkscape, but when importing inkscape imports a big frame with only the top left drawn. The rest is blank. Do I have to change anyvalue in my gnuplot script or do something else with inkscape? I tried to open it in inskcape windows and ubuntu versions, and in both cases it happens the same with the same file.
The problem may be that you have not actually asked gnuplot to produce an eps image. Instead you produced a generic PostScript document with a page size that does not match the default. You need to put the keyword "eps" in your terminal command:
set term postscript eps color size 30,20 font "Times-New-Roman,40"

How to use `set term push` and `set term pop` in gnuplot

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, poping 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 while set term pop restores it.
What am I doing wrong?
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

Is there a blind terminal in gnuplot to use for gathering x/y min/max without a screen plot

Using a flow where I do a 'scratch' plot first to then gather the 'GPVAL_DATA_Y_MIN / MAX' variables for framing the plot size. I then send the actual plot into PNG file. Still the 'scratch' plot command flashes up on screen which I want to avoid. I was doing a 'help set terminal' to see the available terminals (in my gnuplot session) and was looking for something like 'blind' or 'null' but couldn't find any like that. Is there such a terminal? And what is it's name? (Using gnuplot 4.6 patchlevel 7)
Thanks,
Gert
Since I don't know exactly, how you actually use those values, here are some different possibilities:
Gnuplot's "blind" terminal is called unknown:
set terminal unknown
plot "data.dat"
set terminal pngcairo
set output "output.png"
set yrange[GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
replot
Variants of this would be to wrap the set terminal unknown call in set terminal push and set terminal pop to go back to the previous terminal.
Use the stats command:
f = "data.dat"
stats f using 2 nooutput
set yrange [STATS_min:STATS_max]
plot f
If you don't need the values for computations, but only to fit the yrange to your actual data range, then use
set autoscale yfix
or
set autoscale yfixmax
possibly combined with set offsets.
Try to send the output to the null device:
On Linux:
set terminal png
set output "/dev/null"
plot sin(x)
set output "real_output.png"
...
On Windows:
terminal png
set output "nul"
plot sin(x)
set output "real_output.png"
...
Read this SO question or this Wikipedia entry, especially for Windows details.

Gnuplot shows garbage when trying to save picture

When I input the following into the gnuplot window:
set xlabel "x"; set ylabel "y";
plot "rk1000.dat" with lines, "teor1000.dat" with lines
The result is a nice plot. However, when I attempt to save it as a .png file, the result is not so nice. After typing
set xlabel "x"; set ylabel "y"; set terminal png size 800,600;
plot "rk1000.dat" with lines, "teor1000.dat" with lines
the result is gnuplot spitting out garbage characters and doing nothing. Here's a screenshot:
How do I fix this? Not even an hour ago I had no problem saving my plots.
You can use set output in your script to make gnuplot redirect the output to a file:
set terminal png size 800,600
set output "plot.png"
plot "rk1000.dat" with lines, "teor1000.dat" with lines
If you are creating png images using gnuplot, the "pngcairo" terminal produces better-looking results. Try set terminal to see the list of available terminals.
Just save the output to a png file:
gnuplot script.gp > picture.png
Search the web for "redirection" if you want to know more.
you should plot first then set term
ex
"plot '../log/test_coverage_${atpg_mode}.log.tmp' title 'Ori ATPG' w lp lt 3 pt 9"
"set terminal png font \"/usr/share/fonts/liberation/LiberationSerif-Italic.ttf\" 9" ; # save the image as the png format
"set terminal png size 1000, 800" ; # save the image size as 800*1000
"set output '${REPORT_DIR}/Test_Coverage_Curve_${atpg_mode}.png'"
"replot"

gnuplot multiple graphs are not interactive

I am drawing multiple graphs via a shell script in gnuplot.
The graphs are drawn correctly, but am not am able to zoom in . Does any variable need to be set?
Here is the code:
--- for loop of script starts---
gnuplot -persist <<EOF
set term x11 1
set title "IP : $ip Upstream capacity:$UP_CAP kbps"
plot 'trace-0-dir1.txt' using (\$1-$min1):(\$2-\$1-$mindelay1) with lp
set term x11 2
set title "IP: $ip Downstream capacity:$DOWN_CAP kbps"
plot 'trace-0-dir2.txt' using (\$1-$min2):(\$2-\$1-$mindelay2) with lp
EOF
---for loop ends---
Once you switch away from the "x11 1" window the zooming is disabled. To regain control, you should switch back to the specific window (set term x11 1). Another issue is the x11 terminal. You should use wxt which can keep windows alive.
You can solve your issue by using the wxt terminal and separating the two plot commands, therefore not switching away from the window:
--- for loop of script starts---
gnuplot -persist <<EOF
set term wxt
set title "first"
plot x
EOF
gnuplot -persist <<EOF
set term wxt
set title "second"
plot x**2
EOF
---for loop ends---
With this, you have two zoomable windows open and you can still use your shell variables.
In general, you do not have to have console open in order to have active windows, just an appropriate terminal. Especially, the command
gnuplot --persist -e 'plot[0:10] sqrt(x)'
produces scrollable and zoomable windows if used with wxt. Try
gnuplot --persist -e 'set term wxt; plot[0:10] sqrt(x)'
Hope this helps.
AFAIK you are only able to zoom, scroll etc. if the gnuplot console is still active. Meaning, gnuplot must still be running.
To give zooming and scrolling a try, input the following sequence:
Enter the gnuplot console by typing gnuplot
plot a function with plot[0:10] sqrt(x) for example.
Try zooming (Ctrl + Mouse wheel) and scrolling (Mouse wheel / Shift + Mouse wheel) before exiting the gnuplot console.
If you run a script like
gnuplot --persist -e 'plot[0:10] sqrt(x)'
you cannot scroll or zoom anymore.

Resources