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)
Related
Ive installed GNUplot on OSX using
brew install gnuplot
in terminal. I can open gnuplot using
gnuplot
in terminal. I then use
set terminal png
But then if I try to plot anything I just get random characters printed to the screen.
e.g
plot sin(x)/x
returns
Q%#??L?r??T? 89??? ?v?*?`"?Ө~???;???o.?/???jk??W}???f??C8???rR??2j?? GE?e.?fJ??zv!S?f]|???=?^<}?Bq?m??
^<??C&G{nR?Ũ?"~?(YA#?????ϱ??????yd?В->桮???o??~??*-R??m???Q??????Я??=?
ң?9????(Z<?C]???߫???\?wH??Ѧ??(Z?C_???ww???w??ǣ??txE??y???F??q%?n??Q???ǁ|????(Z???X#???V??q3????Pӣ??y?y?e?p???EHK?y????xj?l7?{ݟ?g?1BG?Q?8???g/oD??k#g??h?1}=?n????:????<?????
??Ń'?IEND?B`?gnuplot>
The same thing happens if I set the terminal to jpeg.
gnuplot just prints the contents of a png file that you want to create. You should specify the file name:
set output "myfile.png"
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.
I'd like to redirect gnuplot error messages (that are normally displayed in the gnuplot terminal) to a file for logging. Is there any way to do this?
Example: if I type
gnuplot> Hi!
in the gnuplot terminal, Then I get
gnuplot> Hi!
^
invalid command
gnuplot>
Is there any way to redirect "^\ninvalid command" into another file, e.g. err.txt?
Context: I'm using gnuplot embedded in a c++ application using gnuplot-iostream by Dan Stahlke. It works great! But I have no idea how to get error messages from this pipe, so this would be a good work-around.
I don't know exactly if this applies on your c++ application (probably not), but I thought I'd mention anyway. If you do:
[user#server]$ gnuplot 2> err.txt
gnuplot> Hi!
gnuplot> exit
[user#server]$ cat err.txt
G N U P L O T
Version 5.0 patchlevel 1 last modified 2015-06-07
Copyright (C) 1986-1993, 1998, 2004, 2007-2015
Thomas Williams, Colin Kelley and many others
gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help FAQ"
immediate help: type "help" (plot window: hit 'h')
Terminal type set to 'aqua'
^
invalid command
Maybe you could incorporate something like this. Hope it helps!
I don't think one can achieve this internally within Gnuplot without tinkering with the source code.
The message "invalid command" is produced in command.c by calling the function int_error (defined in util.c) within which is stderr specified explicitly...
It seems gnuplot redirects by default both stdout and stderr to stderr.
Instead, terminal file output is send to stdout. Here is an example to check. The content of tmp.gnu.
print 'some text';
set terminal postscript;
set out
pl sin(x)
gnuplot tmp.gnu 2>tmp.txt 1>sin.ps
tmp.txt contains the stdout and sin.ps the postscript file.
Check also this answer https://stackoverflow.com/a/27375957/11769765 to use
set print "-", set print "/dev/fd/2" or set print "filename" to redirect stdout.
I'm trying to save an .eps file in a folder that doesn't exist yet. I wrote the following gnuplot script:
plot [0:0.13][0:55] 'example/x_-4_U.xy'
set output 'output/x=-4.eps'
replot
The script only works if the "output" folder is already there . Can I create this folder with gnuplot?
Thanks.
You can use the system command:
system "mkdir output"
on Linux, I seem to remember that it is md output on Windows.
Combined with string operators, this is rather flexible:
dir = "output1"
command = "mkdir " . dir
system command
works nicely.
You can concatenate the name of the output file with the folder name:
folderout = 'output/'
plot [0:0.13][0:55] 'example/x_-4_U.xy'
set output folderout.'x=-4.eps'
replot
Sincerely.
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'