is gnuplot fit influenced by terminal? - gnuplot

i have a gnuplot script file to fit measurement data, using this structure:
set terminal png
...some format templates...
f(x) = a + b*x + c*x**2
fit f(x) "datafile.txt" using "X":"Y" via a, b, c
...some plotting commands etc...
with this, gnuplot shows some strange behaviour:
when i run the script as-is, it gives me the following error:
Undefined value during function evaluation
"myscriptfile.gnuplot", line 5: error during fit
when i move the set terminal png line after the fit line, it runs without a hassle.
normally, i'm loading this at the beginning of a master script containing format templates and further data processing routines. doing this also gives me the aforementioned error message, even with the moved set terminal command.
since this is just the first part of processing my data i really need it to work from the master script... i already tried setting initial guesses, FIT_LIMIT and loading it from a gnuplot environment. i'm using gnuplot 4.6.5.
does anyone know how to solve this or how fit gets influenced by other commands? or is this some kind of bug?
edit: uploaded a stripped down version of scripts and data files to here. with the reduced data files the computed fits don't concur with the measured points, but with the complete data they do.

I'm not sure, what the real error is, but it seems to be related to your use of using "PHEAT":"RHOT", although that should be fine.
I could reproduce your error with the following minimal setup:
A data file test.dat:
A B
1 2
2 3
3 4
and a file test.gp:
f(x) = a*x**2 + b*x + c
fit f(x) 'test.dat' using "A":"B" via a,b,c
If I call this file with gnuplot test.gp I get the same error as you. It doesn't appear if I use using 1:2. If I paste the code in an interactive terminal, the error also appears, but only once. If I repeat only the fit command again, it works fine. I'll report this as a bug.
In the script you posted, I was also able to fix this by using using 9:8 instead of using "PHEAT":"RHOT". Additionally you must remove the first line of the data file, which can be done on-the-fly with tail, so that you can leave the using statements of the plot unchanged. So you can use:
fit rhotside(x) "< tail -n +2 testdata.txt" using 9:8 via rhot0, rhot1, rhot2
fit rcoldside(x) "< tail -n +2 testdata2.txt" using 9:8 via rcold0, rcold1, rcold2

Related

gnuplot : writing out particular settings into a "header"

It would be handy to have gnuplot write particular settings to an output file, ideally in a .svg, because svg appears very well suited for that - so the image file and settings are all together - however, it appears this is not feasible directly in gnuplot.
Working within the wxt and svg terminals, and especially multiplot, I have been able to see how gnuplot might interface with the shell (for example):
! ls ; pwd ; echo $0, print sin(pi), show terminal. Trouble happens though when trying usual redirection such as > foo.txt and such - even starting a separate gnuplot session by script just to get the parameters. In particular, I found this interesting :
output is sent to STDOUT
print output is sent to '<stderr>'
... though I'm not sure what to do with that.
I could use e.g. ! cat gnuplot.inp | grep terminal >> gnuplot.svg, and dig into further awk/sed scripting, but before doing that it would help to know if I'm missing any small details in gnuplot first. Thanks.
PS a trivial question : why is the shell in gnuplot sh, when the plain-old linux terminal shell I am using is bash and SHELL=/bin/bash? I notice that shell drops the session into the shell from which gnuplot was executed - not sure if that will help the task.
1 The usual way to save current settings to a file is the command save "filename". There is a script in the gnuplot repository called gpsavediff (it may or may not be included in your distro's gnuplot package) that compares the saved values to the default settings and keeps only the ones that changed. From a gnuplot session under linux a typical use would be
... do a bunch of stuff to make a plot ...
save "| gpsavediff > myplot.gp"
... do a bunch more stuff ...
# recover original plot
reset session
load "myplot.gp"
To write specific lines or text to a file is much simpler than you show. For instance
.
set print "session.log" append
print "# This plot looked good on the screen."
print "# At this point the view angles were ", GPVAL_VIEW_ROT_X, GPVAL_VIEW_ROT_Z
print "# I save a PostScript copy to foo.ps"
set term push
set term postscript color
set output "foo.ps"
replot
unset output
set term pop
...
When invoking a shell, gnuplot uses the libc library function popen(). The gory details of popen() are somewhat system dependent but you probably have a man page for it.
gpsavediff script here

Gnuplot won't open window when called via CPP file

I'm trying to get some practice with C and C++ by playing with gnuplot, but whilst following an example on plotting a simple 2 column data file, I encountered an issue.
When I call gnuplot in my script, it refuses to open any window, and doesn't appear to do any work at all. I don't think this is an issue of window persistence, as I don't even see it open for a split second. I've tried fiddling with setting a persistent terminal and pausing after plotting to no avail. My CPU's activity meter never spikes as it does when I run my other example functions, leading me to believe it never opens in the first place.
However, the strangest part to me is that I managed to plot the data file with gnuplot just how I'd hoped to, but the only way I was able to get it to work was running gnuplot as a terminal command via system().
Below is my function I'm testing with. As the comments note, that system call seems to work perfectly, but I'd prefer to use gnuplot "regularly," not via system calls.
void ex2() {
Gnuplot gp("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\"");
// system("gnuplot -p -e \"plot '2col.dat'\""); #works for some reason?????
gp << "plot '2col.dat'\n";
std::cin.get();
}
I'd appreciate any help you can offer! I just want to be able to plot the file with the "gp <<" notation. I apologize if this is similar to another thread here, but every similar one I've found was solved by adding "pause mouse close" at the end or changing the terminal persist setting, which have not worked for me.

Can gnuplot create the equivalent of an Rstudio script?

I am very new to Gnuplot. I have previously used Rstudio, which has a script (which you can write a command and then edit it later and rerun it) and also a console. Does gnuplot have this script equivalent? I notice after writing a command incorrectly I can't delete it. However, I want to save a document only containing correct commands so I can revisiting the code. Any suggestions would be appreciated.
Gnuplot has a "save " command. The file it produces contains the entire set of commands necessary to reproduce the current state of the program, including the most recent plot. Most of the commands in the saved file are not relevant to that particular plot, however, because they explicitly set a property to a value that was already the default. To remove these unneeded commands you can filter the saved file through a script gpsavediff, which can be downloaded from the gnuplot web site if you don't already have a copy with your installed version. The save + filter operation can either be done in two separate steps:
gnuplot> ... lots of stuff including a plot command ...
gnuplot> save 'myverboseplot.gp'
gnuplot> exit
gpsavediff < myverboseplot.gp > myplot.gp
or it can be done all once using a piped command:
gnuplot> ... lots of stuff ...
gnuplot> save '| gpsavediff > myplot.gp'
You can further edit the commands in the saved script just as you would any other text document.

Avoiding code duplication in gnuplot script

Situation:
I have a rather complicated yet generic gnuplot script to visualize multiple measurement values. However there still is a vast amount of code duplication for the actual plot commands that I want to avoid.
Minimal example (many variables and options omitted) could be as follows:
#define variables here
folder="folder/"
algo="Version1 Version2"
#label:start
#plot first specific e.g. runtime of a program
set output folder."/Runtime".fileext
plot for[i=1:words(algo)] \
folder.word(algo,i).".data" using 1:2 .....
#plot antoher specific e.g. Cache Misses
set output folder."/CacheMisses".fileext
plot for[i=1:words(algo)] \
folder.word(algo,i).".data" using 1:3 .....
#label:end
#change some variables e.g.
algo="Version3 Version4"
#repeat everything between label:start and label:end
In my real script there are about 20 plots in one block (between label:start and label:end) and a high number of blocks (with different variables).
Question:
Is there an easy way to substitute blocks of gnuplot 'code' with a generic or text-replacement method to avoid duplication?
My desired script would look like this:
#define variables here
folder="folder/"
algo="Version1 Version2"
magical_command(...)
#change some variables e.g.
algo="Version3 Version4"
magical_command(...)
I have already found out:
the gnuplot macros do not support variadic texts (i.e. varaibles) inside.
functions need a plot command; which reduces only parts of the duplication
my best guess at the moment would be a call to commandline (e.g. with cmd) to call another gnuplot-script and parameterize the needed variables (Not sure about options though)
I suppose the easiest would be to call the gnuplot script with some argument:
gnuplot -e "algo='Version1 Version2'" yourgnuplotscript.gp
where yourgnuplotscript.gp contains everything between label:start and label:end.
If you need to call this for many algo variables, you could use a bash script with a for loop that runs over those variables.

How to create a data file for gnuplot?

I'm trying to make a graph with gnuplot. I specified my xrange, yrange, and labels, but when I typed in the following command:
gnuplot> plot "data.txt" using 1:2 with lines
gnuplot tells me:
warning: Skipping unreadable file "data.txt" No data in plot.
I don't understand how my data file is unreadable. This is what my data.txt looks like:
X Y [I didn't enter X and Y into my text file]
10000 0.030
5000 0.02
1000 0.012
I know I must be doing something wrong -- this is my first time using gnuplot. I tried doing a Google search on how to make a proper data.txt file turns up zilch.
EDIT:
I feel like this may sound strange to ask at a programming Q&A site, but what should a typical text file w/data look like? I'm no computer programmer, just an undergrad trying to plot a graph for her biochemistry class.
Either as most people answered: the file doesn't exist / you're not specifying the path correctly.
Or, you're simply writing the syntax wrong (which you can't know unless you know what it should be like, right?, especially when in the "help" itself, it's wrong).
For gnuplot 4.6.0 on windows 7, terminal type set to windows
Make sure you specify the file's whole path to avoid looking for it where it's not (default seems to be "documents")
Make sure you use this syntax:
plot 'path\path\desireddatafile.txt'
NOT
plot "< path\path\desireddatafile.txt>"
NOR
plot "path\path\desireddatafile.txt"
also make sure your file is in the right format, like for .txt file format ANSI, not Unicode and such.
plot "data.txt" using 1:2 with lines
works for me. Do you actually have blank lines in your data file? That will cause an empty plot. Can you see a plot without data? Like plot x*x. If not, then your terminal might not be set up correctly.
Create your Datafile like this:
# X Y
10000.0 0.01
100000.0 0.05
1000000.0 0.45
And plot it with
$ gnuplot -p -e "plot 'filename.dat'"
There is a good tutorial: http://www.gnuplotting.org/introduction/plotting-data/
For future reference, I had the same problem
"warning: Skipping unreadable file"
under Linux. The reason was that I love using Tab-completing and in gnuplot this added a whitespace at the end that I did not really notice
gnuplot> plot "./datafile.txt "
I had the same issue when tried to open the file using Plot->Data filename... option provided in the version for Windows 7 (by the way, it worked fine on another computer with the same version of the OP system).
Then I tried to change directory and save the .plt file, but it didn't work either. Finally, I tried to tape manually as it was showed for Linux earlier in this queue of posts:
gnuplot > plot "./datafile.dat"
and it worked!
This error usually means the file couldn't be found.
Can you see the file from the command line?
Try specifying the full pathname.
check line ending type (use 0x0d).
is file open in another program?
do you have read access to it?
I was having the exact same issue. The problem that I was having is that I hadn't saved the .plt file that I was typing into yet. The fix: I saved the .plt file in the same directory as the data that I was trying to plot and suddenly it worked! If they are in the same directory, you don't even need to specify a path, you can just put in the file name.
Below is exactly what was happening to me, and how I fixed it. The first line shows the problem we were both having. I saved in the second line, and the third line worked!
gnuplot> plot 'c:/Documents and Settings/User/Desktop/data.dat'
warning: Skipping unreadable file c:/Documents and Settings/User/Desktop/data.dat
No data in plot
gnuplot> save 'c:/Documents and Settings/User/Desktop/myfile.plt'
gnuplot> plot 'c:/Documents and Settings/User/Desktop/data.dat'
Just go to the properties of your cmd.exe shortcut and change the 'start in' by adding the file name where you put all your '.txt' files.I had same problems and i put the whole file mane as 'D:\photon' in the 'start in' of the properties and it worked.Remember you have to put all your files in that folder otherwise you have to create many shortcuts for each data files.Sorry for late reply

Resources