Hello Is there a way to completely reset gnuplot, ie taking gnuplot in the same state as just after launching it ? Reset does not kill the variables nor the functions, undefine cannot be used as "undefine *" since the first character has to be a letter.
Thank you !
gnuplot> x=1
gnuplot> print x
1
gnuplot> reset session
gnuplot> print x
undefined variable: x
Related
Quite a basic question, I guess, but neither Google nor the docs helped me much so: is it possible to undefine variables? So that
a = 0
[some operation]
print(exists("a"))
results in 0 being printed.
What you are looking for is undefine:
gnuplot> print a
undefined variable: a
gnuplot> print(exists("a"))
0
gnuplot> a=10
gnuplot> print a
10
gnuplot> print(exists("a"))
1
gnuplot> undefine a
gnuplot> print a
undefined variable: a
gnuplot> print(exists("a"))
0
gnuplot>
I have a gnuplot script (template) which looks like this (a little bit shortened):
#!/bin/bash
F1=file_1
F2=file_2
pdffile=output.pdf
#
term="terminal pdfcairo enhanced fontscale .3 lw 3"
out="output '$pdffile'"
#
gnuplot -persist << EOF
#
set $term
set $out
#
call "statistic-file.txt"
#
# ... (some formating instructions removed)
#
plot "$F1" w l lt 1 lc rgb "red" t "Graph1" ,\
"$F2" w l lt 1 lc rgb "green" t "Graph2"
EOF
okular $pdffile
F1 F2 are variables for my data files. Via "call" command i try to include "statistic-file.txt" in which the variables F1 F2 are also used. This file looks like this (also shortened):
#
stats "$F1" u 2 nooutput name "Y1_"
stats "$F1" u 1 every ::Y1_index_min::Y1_index_min nooutput
X1_min = STATS_min
stats "$F1" u 1 every ::Y1_index_max::Y1_index_max nooutput
X1_max = STATS_max
#
# etc
#
set label \
front center point pt 6 lc rgb "red" ps 1.0 \
at first X1_max,Y1_max tc rgb "red" \
sprintf("%.0f kW / %.0f°", Y1_max, X1_max)
Executing the script, i get a error message:
"statistic-file.txt", line 2: Invalid substitution $F
Pasting the content of "statistic-file.txt" into the template file, then it works. It looks as if the variables in the second file have no relation to the variables in the template file. I prefer the 2 files solution, but how to solve it? Any help?
You are mixing bash variables and gnuplot variables. $F1 is a bash variable and will be substituted by bash only within your bash script. Within "statistic-file.txt", bash substitutes nothing, and gnuplot complains about the unknown $F1.
You can try to "convert" the bash variable $F1 into a gnuplot variable datafile1 like this:
#!/bin/bash
F1="file_1"
gnuplot -persist << EOF
datafile1="$F1"
call "statistic-file.txt"
plot datafile1 w lp
EOF
with the following statistic-file.txt:
stats datafile1
Then bash replaces $F1 with the respective filename and gnuplot uses its own variable datafile1, even within the subsequently called "statistic-file.txt".
Just for completeness: The error message "Invalid substitution $F" comes from gnuplot 4, gnuplot 5 complains about "no datablock named $F1".
One solution would be probably to just move the definition of those variables into the Gnuplot template which is being generated and use them directly, i.e.,
#!/bin/bash
pdffile=output.pdf
#
term="terminal pdfcairo enhanced fontscale .3 lw 3"
out="output '$pdffile'"
#
gnuplot -persist << EOF
#
F1="file_1"
F2="file_2"
set $term
set $out
#
call "statistic-file.txt"
#
# ... (some formating instructions removed)
#
plot F1 w l lt 1 lc rgb "red" t "Graph1" ,\
F2 w l lt 1 lc rgb "green" t "Graph2"
EOF
Similar changes would be necessary also in the file statistic-file.txt, e.g., stats F1 u 2 nooutput name "Y1_" instead of stats "$F1" u 2 nooutput name "Y1_".
Hy guys!
I have problems with writing bash script to run 50 times my script which generates data files and then plot it to file.
I wrote smth like this, but it doesnt work
#!/bin/bash
for i in {1..50}
do
./ampl ampltst1 # generates different res.txt file each time
/usr/bin/gnuplot <<\__EOF
set xrange [-2:2]
set yrange [-2:2]
set term png
set output "image-${i}.png"
plot "res.txt" u 1:2 w lines, "res.txt" u 3:4 w lines, "res.txt" u 5:6 w li$
pause -1
__EOF
done
Please help me to fix this script!
Possibly you have problems with indentation: __EOF must be without any leading spaces:
...
/usr/bin/gnuplot <<\__EOF
set xrange [-2:2]
...
__EOF
done
Also \ symbol is not required.
Also content of HERE-IS-DOCUMENT will be indented. Is that OK for gnuplot?
If no, you must remove indentation:
for i in {1..50}
do
./ampl ampltst1 # generates different res.txt file each time
/usr/bin/gnuplot <<__EOF
set xrange [-2:2]
set yrange [-2:2]
set term png
set output "image-${i}.png"
plot "res.txt" u 1:2 w lines, "res.txt" u 3:4 w lines, "res.txt" u 5:6 w li$
pause -1
__EOF
done
Is there any way to iteratively retrieve data from multiple files and plot them on the same graph in gnuplot. Suppose I have files like data1.txt, data2.txt......data1000.txt; each having the same number of columns. Now I could write something like-
plot "data1.txt" using 1:2 title "Flow 1", \
"data2.txt" using 1:2 title "Flow 2", \
.
.
.
"data1000.txt" using 1:2 title "Flow 6"
But this would be really inconvenient. I was wondering whether there is a way to loop through the plot part in gnuplot.
There sure is (in gnuplot 4.4+):
plot for [i=1:1000] 'data'.i.'.txt' using 1:2 title 'Flow '.i
The variable i can be interpreted as a variable or a string, so you could do something like
plot for [i=1:1000] 'data'.i.'.txt' using 1:($2+i) title 'Flow '.i
if you want to have lines offset from each other.
Type help iteration at the gnuplot command line for more info.
Also be sure to see #DarioP's answer about the do for syntax; that gives you something closer to a traditional for loop.
Take a look also to the do { ... } command since gnuplot 4.6 as it is very powerful:
do for [t=0:50] {
outfile = sprintf('animation/bessel%03.0f.png',t)
set output outfile
splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1
}
http://www.gnuplotting.org/gnuplot-4-6-do/
I have the script all.p
set ...
...
list=system('ls -1B *.dat')
plot for [file in list] file w l u 1:2 t file
Here the two last rows are literal, not heuristic. Then i run
$ gnuplot -p all.p
Change *.dat to the file type you have, or add file types.
Next step: Add to ~/.bashrc this line
alias p='gnuplot -p ~/./all.p'
and put your file all.p int your home directory and voila. You can plot all files in any directory by typing p and enter.
EDIT I changed the command, because it didn't work. Previously it contained list(i)=word(system(ls -1B *.dat),i).
Use the following if you have discrete columns to plot in a graph
do for [indx in "2 3 7 8"] {
column = indx + 0
plot ifile using 1:column ;
}
I wanted to use wildcards to plot multiple files often placed in different directories, while working from any directory. The solution i found was to create the following function in ~/.bashrc
plo () {
local arg="w l"
local str="set term wxt size 900,500 title 'wild plotting'
set format y '%g'
set logs
plot"
while [ $# -gt 0 ]
do str="$str '$1' $arg,"
shift
done
echo "$str" | gnuplot -persist
}
and use it e.g. like plo *.dat ../../dir2/*.out, to plot all .dat files in the current directory and all .out files in a directory that happens to be a level up and is called dir2.
Here is the alternative command:
gnuplot -p -e 'plot for [file in system("find . -name \\*.txt -depth 1")] file using 1:2 title file with lines'
Depending on various factors i may not have 1 or more data files, referenced in pre-defined gnuplot plot instructions, that don't exist. When this is the case i get "warning: Skipping unreadable file" which cancels the rest of the instructions.
Is there any way i can ask gnuplot to skip any missing data files and plot all of the existing ones?
Here is a similar solution without a helper script
file_exists(file) = system("[ -f '".file."' ] && echo '1' || echo '0'") + 0
if ( file_exists("mydatafile") ) plot "mydatafile" u 1:2 ...
the + 0 part is to convert the result from string to integer, in this way you can also use the negation
if ( ! file_exists("mydatafile") ) print "mydatafile not found."
Unfortunately, I can't seem to figure out how to do this without a simple helper script. Here's my solution with the "helper":
#!/bin/bash
#script ismissing.sh. prints 1 if the file is missing, 0 if it exists.
test -e $1
echo $?
Now, make it executable:
chmod +x ismissing.sh
Now in your gnuplot script, you can create a simple function:
is_missing(x)=system("/path/to/ismissing.sh ".x)
and then you guard your plot commands as follows:
if (! is_missing("mydatafile") ) plot "mydatafile" u 1:2 ...
EDIT
It appears that gnuplot isn't choking because your file is missing -- The actual problem arises when gnuplot tries to set the range for the plot from the missing data (I assume you're autoscaling the axis ranges). Another solution is to explicitly set the axis ranges:
set xrange [-10:10]
set yrange [-1:1]
plot "does_not_exist" u 1:2
plot sin(x) #still plots