Use gnuplot command without prompt - linux

To use the Gnuplot (On linux Ubuntu 16.04) command directly by specifying arguments without using the its prompt, I enter this for example :
gnuplot -e "filename='DataFile.txt'" SettingsFile
Where SettingsFile looks like this :
plot filename with lines
set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","
pause -1
And the DataFile.txt looks like this :
Packets, Time(in seconds) :
13392,120
24607,240
23867,360
21764,480
20727,600
20004,720
19719,840
19758,960
19728,1080
20168,1200
19737,1320
19729,1440
20135,1560
20006,1680
21301,1800
19923,1920
20002,2040
19761,2160
20918,2280
22756,2400
22820,2520
23370,2640
22987,2760
22956,2880
24427,3000
23527,3120
24009,3240
23832,3360
23464,3480
23652,3600
11212,3654
First question :
Is there a way to set into that SettingsFile a png OutputFile ? So I can enter it as an argument to the Gnuplot command just as I did with the DataFile. (I want to use it this way, because I want to invoke it from an external code)
I want to achieve something like this :
gnuplot -e "filename='DataFile.txt'" SettingsFile OutputFile.png
Second question :
The screen output that I get from Gnuplot shows the xtics differently than expected :
Notice also that the axis titles are not shown !
Now if I try to resize the window I get this :
The graph gets bizarrely flipped, with the titles set and the tics being updated as desired.
How should I fix these two problems, first mentioning an output file in the SettingsFile, and second the xtics not being showed properly and third this strange behavior in the screen output ?

Several commands can be added to gnuplot -e through semicolons, for example:
gnuplot -p -e 'filename="data.txt"; fileout="image.png"' SettingsFile
Your SettingsFile should already have a line configuring the terminal type:
set terminal png
set output fileout
set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","
plot filename using 2:1 with lines
If you want more control over your code, try with this script (gnuplot 5.0+):
filename=(ARGC>0 ? ARG1 : 'DataFile.txt' ) # By default filename=DataFile.txt
# If called with one argument (ARGC=1)
# then `filename=ARG1`
if(ARGC>1){
# if called with two arguments (ARGC=2), then configure a png output
set terminal png
set output ARG2
}
set title "Total Packets consumption"
set xlabel "Time"
set ylabel "Packets"
set datafile separator ","
# the plot command ALWAYS at the end of the script after the settings
plot filename using 2:1 with lines
If you want to plot 'DataFile.txt' (by default) interactively:
gnuplot -p SettingsFile
If you want to plot another file, e.g. AnotherData.txt:
gnuplot -p -c SettingsFile AnotherData.txt
If you want to plot another file and save it as PNG:
gnuplot -p -c SettingsFile AnotherData.txt Output.png
The -p argument lets plot windows survive after main gnuplot program exits. Thw -c argument load script using gnuplot's "call" mechanism and pass it the remainder of the command line as arguments. See How to pass command line argument to gnuplot?
Notice that your script plots the datafile first, and THEN configure the labels, title and datafile separator. That is why you see weird tics.

Related

Strange tex files management in Gnuplot with tikz term

I fount a strange phenomenon. If we run the following gnuplot script (in a folder with gnuplot-lua-tikz-common.tex, gnuplot-lua-tikz.sty, gnuplot-lua-tikz.tex, t-gnuplot-lua-tikz.tex)
tikzfile="test.tex"
plot x**2
set term tikz standalone monochrome
set output tikzfile #
replot #
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)
we found the following fatal error
! Emergency stop.
<*> test.tex
! ==> Fatal error occurred, no output PDF file produced!
Anyway we have the test.tex file. Hence if we re-run the same script with the #-marked lines commented we obtain no error and the perfect test.pdf file.
During the first exec, with set term we have an empty file, with replot we fill it, but until the end of the exec we can't use it as an input of pdflatex. Why?
During the second exec we already have the test.tex file, so if we comment set term and replot we can use it as an input of pdflatex. Why?
Thank you.
Gnuplot doesn't automatically flush and finish an output file after a plot.
So, if you want to further process an output file from within the gnuplot script you must explicitly close the file with set output beforehand:
tikzfile="test.tex"
set term tikz standalone
set output tikzfile
plot x**2
set output
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)

gnuplot emits bad file format error when set table command is used

I'm trying to get part of the answer in https://stackoverflow.com/a/27534509/257924 to work but am running into a Bad format character error.
Only when I comment out the set table command below, do I get the error (execute this as a script on Linux). Below, I'm expecting /tmp/temporaryfile.dat to be generated and not a plot image file:
#!/bin/bash
datafile=/tmp/mydata.1
cat > $datafile <<EOF
2013/5/1 1
2013/5/1 1
2013/5/1 1
2013/6/1 2
2013/6/1 2
EOF
gnuplot_in=/tmp/gnuplot.in
gnuplot_out=/tmp/gnuplot.png
cat > $gnuplot_in <<EOF
set xdata time
set timefmt '%Y/%m/%d'
set table '/tmp/temporaryfile.dat'
set format x '%Y/%m/%d'
plot "$datafile" using (timecolumn(1)):2 title "thetitle" with linespoints;
unset table
EOF
# Show the input with line numbers for debugging:
grep -n '.*' $gnuplot_in
gnuplot $gnuplot_in
#eog $gnuplot_out
The error is:
1: set xdata time
2: set timefmt '%Y/%m/%d'
3: set table '/tmp/temporaryfile.dat'
4: set format x '%Y/%m/%d'
5: plot "/tmp/mydata.1" using (timecolumn(1)):2 title "thetitle" with linespoints;
6: unset table
"/tmp/gnuplot.in", line 5: Bad format character
I've already checked the script for accidental UNICODE characters as indicated by gnuplot error: Bad format character
What would cause this error and how do I fix it?
I've concluded that this is due to a bug in the version of gnuplot on my RHEL 6.6 machine. On my RHEL 6.6 machine we have this version of gnuplot:
gnuplot 4.2 patchlevel 0
On my Debian system, the script works without errors, using this version of gnuplot:
gnuplot 4.6 patchlevel 0

Plot all files in a directory simultanously with gnuplot?

I want to do something similar to this question: gnuplot : plotting data from multiple input files in a single graph.
I want to plot simultaneously all the files in a directory, without having to explicitly write their names. The column numbers are the same for all the files. What can I do?
Doing plot for [file in *] file u 3:2 doesn't work.
Also, I don't want each file to have a different legend. All points from all files should be treated the same, as if they all came from a single file.
As an alternative to Jonatan's answer, I would go with
FILES = system("ls -1 *.dat")
plot for [data in FILES] data u 1:2 w p pt 1 lt rgb 'black' notitle
or
plot '<(cat *.dat)' u 3:2 title 'your data'
The first option gives you more flexibility if you want to label each curve. For example, if you have several files with names data_1.dat, data_2.dat, etc., which will be labeled as 1, 2, etc., then:
FILES = system("ls -1 data_*.dat")
LABEL = system("ls -1 data_*.dat | sed -e 's/data_//' -e 's/.dat//'")
plot for [i=1:words(FILES)] word(FILES,i) u 3:2 title word(LABEL,i) noenhanced
You could try something like:
a=system('a=`tempfile`;cat *.dat > $a;echo "$a"')
plot a u 3:2
This uses the command line tempfile command to create a safe, unique, and disposable temporary file. It mashes all of the data files into this file. It then echoes the file's name so gnuplot can retrieve it. Gnuplot then plots things.
Worried about header lines? Try this:
a=system('a=`tempfile`;cat *.dat | grep "^\s*[0-9]" > $a;echo "$a"')
The regular expression ^\s*[0-9] will match all lines which begin with any amount of whitespace followed by a number.
I like to be able too choose the files to plot with wildcards, so if you like that you can do as follows, though there are many ways. Create the following script.
script.sh:
gnuplot -p << eof
set term wxt size 1200,900 title 'plots'
set logs
set xlabel 'energy'
plot for [ file in "$#" ] file w l
eof
do chmod u+x script.sh
Run like ./script.sh dir/* *.dat
If you need it often make an alias for it and put it in some reasonable place:)
Cheers /J
Try the following command:
gnuplot -e 'plot for [file in system("find . -depth 1 -type f -print")] file u 3:2'
Note: Add -p to keep the plot window.

plot is not generated (gnuplot from command line)

In sample.dat I have:
set terminal pngcairo transparent enhanced font "arial,10" fontscale 1.0 size 500, 350
set output 'simple.2.png'
plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))
From ubuntu/terminal I execute:
gnuplot -e "filename='sample.dat'"
but the simple.2.png file is not created. How do I export a plot to e.g png using gnuplot from command line?
I have no idea where you got that from!
To execute a gnuplot script (here called simple.gp), simply call
gnuplot simple.gp
You should keep the extension .dat for data files.

Gnuplot: plot data from command

I know I can plot with data from stdin by using '-' as the data source, but is there any way I can plot data output from a command the same way? I.e., instead of running the command and piping to the gnuplot script, can I specify the command in the gnuplot script itself?
Something like this (but this doesn't work):
set terminal pngcairo
set output 'test.png'
cmd = "`./mycmd`" # running ./mycmd in terminal gives my data to stdout.
# the command can be several other commands, piped together.
# i'm only interested in whatever comes to stdout after running
# the entire thing.
plot cmd u 2:3 w lines # etc...
The above makes cmd contain a single long line with all the lines of output smashed together.
Yes, you can:
plot '< ./mycmd'

Resources