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.
Related
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?
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.
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?
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.
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.)