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?
Related
I'd like to make gnuplot scripts to output *.eps files and change them into .pdf type automatically. I have a test demo which goes as:
set term postscript eps enhanced color
set output "SystemCommand.eps"
plot sin(x)
set xl "x"
set yl "y=sin(x)"
system(sprintf("epstopdf %s",SystemCommand.eps))
but it doesn't produce what I want, an error comes out every time that says:
"SystemCommand.gp", line 6: undefined variable: SystemCommand
I have tried to use SystemCommand or SystemCommand.eps, no difference here. Anyone has a suggestion?
I use Ubuntu 14.04 and bash shell.
You need to quote the filename in sprintf.
set term postscript eps enhanced color
set output "SystemCommand.eps"
plot sin(x)
set xl "x"
set yl "y=sin(x)"
system(sprintf("epstopdf %s","SystemCommand.eps"))
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.)
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.
I have some gnuplot code as follows:
gnuplot> h=1
gnuplot> a=2
gnuplot> set print "additional numbers.txt"
gnuplot> print h,a
gnuplot> set print
Now I want to add more data to this file without overwritng the previous entry. I can do it in C, but I want to keep everything in 1 script that I can run through gnuplot
I hope you can help.
Use the append flag:
set print "additional_numbers.txt" append