gnuplot generate plot command with sprintf? - gnuplot

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.

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' ...).

Drag and drop script for gnuplot

I have some data I would like to plot in gnuplot. For being able to plot them I wrote a script called "plot_A.gp". In it I set the axis values, labels and the output terminal.
In order not to have it modificated for every data file, I was wondering if it is possible to make the script able to handle drag and drop-files.
As example script:
set xrange [0 to 100]
set xlabel "x-axis"
set ylabel "y-axis"
set terminal eps
set output %1.pdf
plot %1 using 1:($2*$2) with lines
How can I set %1 to the file name I just dropped onto the script?
Please execute the script with call instead of load at the gnuplot prompt. call accepts upto 10 arguments. Before gnuplot 5.0, these arguments are called $0, $1, ..., $9.
In gnuplot 4.6, your script would look like this:
datafile="$0"
outputfile="$0".".pdf"
set xrange [0 to 100]
set xlabel "x-axis"
set ylabel "y-axis"
plot "hello" using 1:($$2*$$2) with lines
set terminal eps
set output outputfile
replot
set terminal wxt
set output
Because $2 refers to the third parameter to the script, to access the second column use $$2 instead. Alternately, you can also use 1:(column(2)*column(2)).
You would call it like this.
gnuplot> call "plot_A.gp" "hello"
This will plot the data in the file "hello" and create a pdf called "hello.pdf". I have also reset the terminal back to wxt, as a best-practice.
From gnuplot 5.0, using $0, $1 etc is deprecated. Instead you should use the special variables ARG0, ARG2, ..., ARG9. I don't have access to gnuplot 5.0. But I think you just need to use ARG0 instead of $0. Please you could refer to this answer to How to pass command line argument to gnuplot?

Gnuplot error on command "set data s l"

My program has gnuplot script and its first line is
set data s l
But this makes error
"cdia.GNUBAND", line 1: unrecognized option - see 'help set'.
I think it's the version problem and I have to change the "set data s l" command in right way. I searched on web but couldn't find it.. How do I solve this?
That is the shortcut for set data style lines, which is deprecated since version 4.0 (released in 2004). To replace this line, use
set style data lines
This sets the default plotting style for data set to lines, i.e. the commands
set style data lines
plot 'data.dat'
and
plot 'data.dat' with lines
are equivalent.

gnuplot - can I put all the settings for a plot in a file, and call it before the plot command?

I would like to save the set commands used to define my graph.
Basically I would like to save the style of the line, the color, output name, ranges and so on; in a file
Then I would like to call it from a script, like
gnuplot <setting script name> <data file> "plot <plot commands>"
Is this possible? The manual don't mention any kind of capability to load the options that you set for the plot function, from an external file.
There are several approaches.
You can store very general settings in a ~/.gnuplot file.
Otherwise, you can make a script, named myplot, which constructs it locally
#!/bin/bash
gnuplot << EOF
<all settings>
plot "$1"
EOF
Which you call by ./myplot <datafile>
You can, e.g. put all settings in one file e.g. settings.gp
set terminal ...
set xtics ...
Have one file plot.gp which contains all plotting commands which use a variable datafile:
plot datafile using 1:2
The variable datafile can be given using the -e option, and the settings file can be loaded with load
gnuplot -e "datafile='mydatafile.dat'; load 'settings.gp'" plot.gp
See also How to pass command line argument to gnuplot?

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.

Resources