Gnuplot: plot data from command - gnuplot

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'

Related

Use gnuplot command without prompt

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.

plot multiple datasets with gnuplot script from stdin

I am trying to plot some data with gnuplot. I want to use a static script.gp file and feed it from stdin with my data. I also have multiple datasets that I need to pass to the script.
script.gp:
set term jpeg
set encoding utf8
plot '<cat' index 0 with lines, plot '' index 1 with lines
data:
1 2
2 2
0 0
7 4
command:
cat data | gnuplot script.gp
This doesn't work, since I'm guessing it tries to reread from stdin. Is there a way I can do this, or do I have to use temporary files to store my data?
The solution I 've found so far has plenty of drawbacks, however it kind of does the job:
Use 'gnuplot -e' and cat the script file into the command:
cat data | gnuplot -e "$(cat script.gp)"
change script.gp using ; at the end of every line, remove all comments and change the plot command using '-' instead of '<cat' and removing index:
set term jpeg;
set encoding utf8;
plot '-' with lines, '-' with lines
change the data format, seperating each dataset with a line with e:
1 2
2 2
e
0 0
7 4
I would suggest to use the special file '<' which allows you to call the php script and you can have your gnuplot file plot.gp:
set term jpeg;
set encoding utf8;
set output file_out
my_cmd1=sprintf('< my_script %s', my_file1)
my_cmd2=sprintf('< my_script %s', my_file2)
plot my_cmd1 with lines, my_cmd2 with lines
and you call in a shell with:
gnuplot -e 'my_file1="input1.txt"; my_file2="input2.txt"; file_out="my_out.jpg"' plot.gp

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)

Inserting commands from another file into a gnuplot script in a bash environment

In my bash script, I am doing a bunch of operations before starting my gnuplot script. My bash script looks like this:
#!/bin/bash -e
# Do some bash operations here
# Create a file containing a list of Gnuplot commands called arrow_coords
# All the commands are the same: just drawing a group of arrows
# Every line in "arrow_coords" looks like this:
# set arrow from -500,-100 to 500,-100 as 1
# There are hundreds of those line commands
# Entering Gnuplot script
gnuplot <<- EOF
reset
set terminal pngcairo transparent nocrop enhanced font '$font_type, 22' size 1800,1800
set output "$output_file"
set yrange [-1:18]
set xrange [-1:18]
? <----------------------------------- HOW TO INSERT COMMANDS FROM ANOTHER FILE?
plot '$dat_file' using 1:2 with points ls 1
EOF
I could not find a way to insert the commands written in arrow_coords into the Gnuplot script I have in my bash file. Is this possible? Are there other suggestions to what I am trying to do?
If your file contains only gnuplot instructions, you can run it with the load or call commands:
gnuplot <<- EOF
# your gnuplot configurations
load 'arrow_coords' # calling external file
plot '$dat_file' using 1:2 with points ls 1
EOF
Here's an example that illustrates the solution:
#!/bin/bash
# prepare file
echo "Test!" > test.txt
a=`cat test.txt`
cat <<- EOF
File contents: $a
Again: `cat test.txt`
EOF
So in your code, you could replace the line starting with ? with:
`cat the_file_you_generated`

gnuplot command line arguments and call arguments

following up from the question on SO about command line parameters to gnuplot scripts, and its corresponding answer, I see that I can make a script myscript.gp like
if(!exist("parameters")) parameters=""
plot "< myprog ".parameters u 1:2
and call it with
$ gnuplot -e "parameters='-m 5'" myscript.gp
However, I could achieve a similar result when running in interactive mode with myscript2.gp as
parameters="$0"
plot "< myprog ".parameters u 1:2
and calling it from the interactive prompt as
gnuplot> call "myscript2.gp" "-m 5"
And now the question. As I see it those two methods are not connected and the $O would fail when when running in batch, whereas the 'parameters' is not updated when using call.
How do I set a script which could be called from the shell command line AS WELL AS from the gnuplot interactive command line? Another way to ask it, is it possible to test in which conditions was the script called?
Thanks,
Playing around, I found a quite inelegant solution, but that could help to achieve what I want.
parameters="" # sets default value
if ("$#" eq sprintf("%d",1)) parameters="$0" # if using call with 1 parameter: $#=1
if (exist("bpar")) parameters=bpar # checking for batch call
plot "< myprog ".parameters u 1:2
which can then be called by either
$ gnuplot -e "bpar='-m 5'" myscript.gp
or
gnuplot> call "myscript2.gp" "-m 5"

Resources