gnuplot bash script variables [duplicate] - linux

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
I have been creating a quick bash script that will generate some resource numbers and display them via gnuplot. I ran into an issue the second I changed the filenames in the gnuplot command to reflect a variable my script sets up for file location. Example code is below.
Any idea why I am having this issue? I am guessing that gnuplot is not expanding my variable I setup, I just cannot figure out what I need to change. Thank you.
testFile=/var/log/testing.log
testFileTwo=/var/log/testingTwo.log
gnuplot -persist -e 'set xlabel "TIME"; set ylabel "PERCENT" ; set yrange [0:100]' -e 'plot ${testFile} smooth bezier, ${testFileTwo} smooth bezier'
As soon as I run this script, I receive the following error.
plot ${testFile} smooth bezeri, ${testFileTwo} smooth bezier
^
line 0: invalid complex constant

Bash does not expand variables inside ' single quotes. If you use " double quotes after the second -e, bash will expand ${testFile} and ${testFileTwo} before passing the resulting string to gnuplot.
EDIT: use -e "plot '${testFile}' ...", to make sure that plot receives the name inside quotes.

Related

Is using a gnuplot variable in an external datafile manipulation command possible?

Using an external datafile manipulation cmd in gnuplot looks like
plot '<(grep "1" "/path/datafile")' using 1:2 ...
Can we use a gnuplot variable in such a cmd like in
plot for [i=1:5] '<(grep "i" "/path/datafile")' using 1:2 ...
where "i" should be the gnuplot variable i.
Is it possible to hand over the gnuplot variable to the external command and how?
Thx!
You can build a string command using the concatenation operator ".". Eg
plot for [i=1:5] "<(grep '".i."' /path/datafile)" using 1:2 ...
You might want to grep for something less ambiguous than single digits like 1 that are likely to match within any number. For example, add word delimiters like \b, or even just a leading space. This is why I added the single quoting of the value as a preliminary step (we are generating grep '1' ...).

gnuplot generate plot command with sprintf?

I want to generate a gnuplot plot command programmatically, like:
plotline = sprintf("'datafile1.dat' using %d:3 with points, '%s' using %d:3 with points",i,targfile,i)
plot plotline
Where 'plotline' in the second line is expanded to produce and execute a full command like:
plot 'datafile1.dat' using 8:3 with points, 'datafile2.dat' using 8:3 with points
I want to do this in order to echo 'plotline' in the terminal and so be certain exactly what is being shown while cycling through a set of columns / datafiles / whatever inside a loop in a gnuplot script.
Is there / what is the syntax to do this, or can you suggest another approach to report the plot command as executed (without splitting into a plot command and a separate set of commands to report the current variable states).
Thanks!
In order to construct such a plot command from some strings, you can use eval to execute the commands contained in a string:
plotline = 'x title "mytitle"'
eval('plot '.plotline)
Alternatively you can use set macros:
set macros
plotline = 'x title "mytitle"'
plot #plotline
This replaces #plotline with the content of the string variable plotline before executing the command. Using plot plotline interpretes the content of plotline as file name. Note, that as of version 4.6 macros don't work properly in loops, but eval works fine.
BTW: If you don't specify your own title, then the actual plot statement is written in the plot legend. But that can't be written to the terminal output.

How does gnuplot understand command line arguments from stdin?

I generated a graph using gnuplot with following command:
echo 'plot [0:14][0:1000] "source_data_file" with steps title "example graph"; pause 15' | gnuplot
I would like to change the name of the X- and Y-axes with xlabel and ylabel arguments, but if I execute:
echo 'plot [0:14][0:1000] "source_data_file" with steps title "example graph" xlabel 'X-axe label' ylabel 'Y-axe label'; pause 15' | gnuplot
..I receive an error message pointing to xlabel. I have tried to separate arguments with semicolons, but this had no affect. In addition, I would like to change the format of the Y-axe from exponent(for example 1.8232e+06) to integers(for example 1823200), but I don't know how to pass the format y/format x argument to gnuplot.
How does gnuplot understand command line arguments if those are passed to gnuplot from stdin?
#arbautjc is correct--you have a problem with using single quotes for the x and y axis labels. Also, those need to be specified before the plot command is run. So, a better way would be
echo 'set xlabel "X-axe label"; set ylabel "Y-axe label"; plot [0:14][0:1000] "source_data_file" with steps title "example graph"' | gnuplot -p
Also, is the pause command necessary? (It may have some reason to be there, but I cannot see it from your example.)

How to give command line arguments to gnuplot [duplicate]

This question already has answers here:
How to pass command line argument to gnuplot?
(10 answers)
Closed 7 years ago.
I am writing a script to plot multiple files from multiple files one at a time. I also want to save the output of each plot corresponding to the data file. How can we give both arguments to GNUPlot. For example: sample GNUPlot script
set xlabel "a"
set ylabel "b"
set zlabel "c"
set term postscript
set output "??" #'??' implies variable i.e. take file name from commandline
splot "??" with points,"??" with points #'??' implies variable i.e. take file name from commandline
and this script will be run by another shell script generating the required file names.
Any help appreciated.
You can also use the -e command-line option of gnuplot. See this question for an example: How to pass command line argument to gnuplot?
Try this simple bash script
#!/bin/bash
file="output.png"
infile1="$1"
infile2="$2"
echo "
set xlabel \"a\"
set ylabel \"b\"
set zlabel \"c\"
set term postscript
set output \"$file\" #'??' implies variable i.e. take file name from commandline
splot \"$infile1\" with points,\"$infile2\" with points #'??' implies variable i.e. take file name from commandline
" > sample.gp && gnuplot sample.gp
And call it like ./script data.txt data2.txt
You will have the output stored in output.png and the gnuplot file stored in sample.gp file.
It's easy to add more files to plot, even if the number of plotted files is different everytime. You can then also store your .gp file in different output.

Display underscore rather than subscript in gnuplot titles

Short question:
How do I display the _ (underscore) character in a title in gnuplot that is assigned from a variable name in gnuplot?
Details:
I have something like the following code:
items = "foo_abc foo_bcd bar_def"
do for [item in items] {
set title item
set output item.eps
plot item."-input.txt" using 1:2 title item with linespoints
}
This works fine with gnuplot except that the title get changed from foo_abc to fooabc. I don't know if I want to use an escape character because I don't want that to be in the file name. I've tried a couple of different options with single vs. double quotes but I haven't found what I need yet.
Instead of foo_abc, write foo\\\_abc.
Most gnuplot commands which generate labels accept a noenhanced keyword which will prevent gnuplot from using enhanced text for just that string. In this case, it should be sufficient to just do:
set title item noenhanced
An alternative is to create a function which will remove the unwanted text from the string when passing it to set output:
remove(x,s)=(i0=strstrt(s,x),i0 ? remove(x,s[:i0-1].s[i0+strlen(x):]):s)
# Makes me wish gnuplot syntax was more pythonic :-p
#TODO: Write a `replace` function :-). These just might go into my ".gnuplot" file...
I use an inline function to find the index of the first occurrence of x in the string s. I then remove that occurrence via string concatenation and slicing and recursively call the function again to remove the next occurence. If the index isn't found (strstrt returns 0) then we just return the string that was put in. Now you can do:
set output remove('\',item)
set title item
The underscore comes from treating titles as "enhanced text". Turn that off using
set key noenhanced
If you are using the enhanced eps terminal, that is the reason you need to escape the underscore in the first place. There was another related question today which explains the issue a bit. When you set the terminal, try:
set terminal postscript noenhanced <whatever else here...>
That works for me (Arch linux, gnuplot 4.7.0). If the enhanced terminal is essential, below is a partial solution I found. The assumption is that the underscore always appears in the same place in the string.
set terminal postscript enhanced
items = 'foo\_abc foo\_bcd bar\_def'
do for [item in items] {
set output item[1:3].item[5:*].'.eps'
set title item
plot sin(x)
}
This way you can escape the underscore and not have the \ appear in the filename. Note the use of single quotes for the 'items' string; see the previously linked question for details.
I had the same problem about the underscore in the title: such as I needed to write 4_3 subframe and I needed the enhanced postscript. The SIMPLEST way turned out to be from the adjacent post: ``If you are using the enhanced eps terminal, that is the reason you need to escape the underscore in the first place. There was another related question today which explains the issue a bit." - How is # produced in gnuplot?
So, I followed their advice and this worked:
plot 'LC.stats' u 3:4 ti "{/=15 1350 stars in C18 4\_3 subframe}" -
Double escape character before the underscore.

Resources