How does gnuplot understand command line arguments from stdin? - gnuplot

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

Related

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 script to output eps or svg - how to write?

I have the following gnuplot script:
set autoscale
unset log
unset label
set xtic auto
set ytic auto
unset title
set xlabel "Number of clusters"
set ylabel "Accuracy of classifier (%)"
plot "cluster.dat" using 1:3 title "PART" with lines, \
"cluster.dat" using 1:4 title "JRip" with lines, \
"cluster.dat" using 1:5 title "FURIA" with lines
I would like this script, when run, to output SVG or EPS - what would I need to add or modify to make this happen?
In gnuplot the output type is called terminal.
In your script, before the plot command use
set term svg
set output "output.svg"
or
set term eps
set output "output.eps"
both terminals have several options. Type help eps (on some gnuplot this is help epscairo) or help svg.
To get the list of available terminals for your build, type set terminal.

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.

Gnuplot - Using replot with png terminal

I am trying to use replot with png terminal in Gnuplot.
If I do the following I get two plots on one graph without any problem:
plot sin(x)/x
replot sin(x)
Now if do the same for a png terminal type the resulting png file only contains the first plot.
set terminal png
set output 'file.png'
plot sin(x)/x
replot sin(x)
Am I missing something at the end to get the second plot in my png file?
This is actually a very good question, and the behavior here is terminal dependent. Some terminals (e.g. postscript) will give you a new page for each replot. You have a couple of solutions...
First Option: You can make your plot prior to setting the terminal/output and then replot again after you set the terminal/output:
plot sin(x)/x
replot sin(x)
set terminal png
set output 'file.png
replot
This option is sometimes convenient if you want to plot the same thing in multiple terminals, but I rarely use it for anything else.
Second (better) Option: You can pack multiple plots into one command separating each with a comma.
set terminal png
set output 'file.png'
plot sin(x)/x, sin(x)
I very much prefer the second way -- when in a multiplot environment, this is the only way to put multiple graphs on the same plot. If you have very long functions to plot, you can break the line with gnuplot's line continuation (\ at the end of the line -- Nothing is allowed after the \, not even whitespace)
plot sin(x)/x with lines linecolor rgb "blue" linetype 7 lineweight 4, \
sin(x), \
cos(x)

gnuplot v4.4: Problem plotting using timeseries x axis

could someone please help me. I'm trying to create a simple chart.
set datafile separator ","
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set autoscale y
plot ["13:00":"14:00"] './data.csv' using 1:2 with lines
data.csv:
26/10/2010 13:30:01,1
26/10/2010 13:30:12,2
26/10/2010 13:30:23,3
26/10/2010 13:30:34,4
gives me the error:
line 6: all points y value undefined!
I've tried all manners of timefmt settings!
Many thanks
The problem is defining the xrange, it needs to be the same format timefmt (see ?time_axis)
The following works for me
set datafile separator ","
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set autoscale y
set xrange["26/10/2010 13:00:00":"26/10/2010 14:00:00"]
plot './data.csv' using 1:2 with lines
Oh, and I got rid of the blank lines in between each line of data.
If you don't want to edit the file to get rid of the blank data, then you can use awk in gnuplot like so,
plot "<awk '$0!~/^$/ {print $0}' ./data.csv" using 1:2 with lines
for the final command.
EDIT: further info in case there is still a problem (see the comments)
I needed to use the awk command to get the data to plot. This was to remove the blank lines in the data. Combining awk and gnuplot in this fashion works on linux systems, I'm not certain about gnuplot on windows. It could be that certain piping isn't happening, in which case you would have to remove the blank lines before using gnuplot. (you could still use awk for this, but perhaps not in the plot command?)
If you are using linux and the above is not working, then something else is the problem. Perhaps there are old commands stored in the gnuplot session? To make sure we are doing exactly the same thing, I give the shell script that I used (I changed the xrange to fit the data better and make a nicer plot, also notive the \$ instead of $, otherwise the shell misinterprets the $ sign).
Okay: I made the file data.csv.sh in a new folder (in case you have data.csv or data.csv.png files already),:
#!/bin/bash
echo "26/10/2010 13:30:01,1
26/10/2010 13:30:12,2
26/10/2010 13:30:23,3
26/10/2010 13:30:34,4" > "data.csv"
gnuplot<<EOF
set term png small
set output "data.csv.png"
set datafile separator ","
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set autoscale y
set xrange["26/10/2010 13:30:00":"26/10/2010 13:31:00"]
plot "<awk '\$0!~/^\$/ {print \$0}' ./data.csv" using 1:2 with lines
set output
set term pop
EOF
Then on the terminal I typed:
chmod +wrx data.csv.sh && ./data.csv.sh
to make the shell script executable and then to run it.
The file data.csv.png looks like the following
All the best
Tom
I just wanted to add to the above discussion in case someone was having the same issues. I was trying to plot a time series, such as:
07/22/13 01:00 120
When I tried to plot using the above procedure I got the bad time format error. I changed my input data to:
07/22/2013 01:00 120
After the change, it ran perfectly. This would make sense because to gnuplot the 07/22/13 is vague. That is, is it 1913, 1813, or 2013 (or any other yy13).

Resources