in a gnuplot script how to go access the range - gnuplot

I call a function in a gnuplot session, and would like to know the ranges and also if a logscale is used inside this function. How to do that ?
Thank you
gnuplot> load 'myfunction.gnu'
the file myfunction.gnu:
if "logscale" {do this};else {do that}

Partial solution could be as follows. Let's say that the main script looks like:
set logscale x
set xr [10:100]
load 'my_function.gpl'
Then in my_function.gpl, you could do:
set terminal push
set terminal unknown
plot 1
isLogScale = (GPVAL_X_LOG > 0)?1:0
xMin = GPVAL_X_MIN
xMax = GPVAL_X_MAX
set terminal pop
if(isLogScale){
print "logscale"
}else{
print "..."
}
The idea is to:
save the current terminal with set terminal push
set a "fake" terminal with set terminal unknown
generate a plot into this terminal (nothing is plotted) in order to force Gnuplot to initialize its internal variables used in the lines below
check the log scaling using GPVAL_X_LOG (equal to zero if logscale hasn't been set) and ranges with GPVAL_X_MIN, GPVAL_X_MAX.
revert the terminal settings with set terminal pop
perform some action using this information
However, the catch is that although set logscale x sets GPVAL_X_LOG by default to 10, unset logscale does not revert it back to zero, so if one used set logscale x;unset logscale, the code snippet above would not handle this correctly...
In my opinion, a better solution would be to introduce a custom variable in the main script as:
useLogScale = 1
if(useLogScale){
set logscale x
}
set xr [10:100]
load 'my_function.gpl'
then the variable useLogScale is also directly available in my_function.gpl.

Related

Gnuplot - script internal error, but when I write it normally it works, but script not

I don't know why but I have an error. When I write all this things in terminal everything is ok, but when I use script I have this error
gnuplot> load wykres_sin.gp
internal error : STRING operator applied to undefined or non-STRING variable
This is my script
# Skrypt gnuplot
plot sin(x) w l
set title "Wykres sinusa od wartosci kata"
set xlabel "Wartosc kata (stopnie)"
set ylabel "Wartość sinusa"
set xrange [0:750]
set yrange [-1.2:1.2]
set term png large size 800,600
set output "sin.png"
replot
exit
You need quotes around the file name:
gnuplot> load "wykres_sin.gp"

gnuplot - USING string variables

I'm trying something very simple ...
#!/usr/bin/gnuplot
reset
filename = "something_or_other"
set terminal pngcairo dashed size 800,400 enhanced font 'Verdana,10'
set output filename.".png"
set title filename."\n"
set xlabel "probably time"
set ylabel "probably something else" offset graph 0.2,0.6 rotate by 0
plot filename.".dat" using 1:2 lc rgb "#00E80000" title "measurements"
!display filename.".png"
except filename stays just like that and doesn't get interpreted as a string.
How do I get this working?
The exlamation mark ! at the beginning of the line makes gnuplot send this whole line as it is to a shell. So, here you cannot use any gnuplot variables and string concatenation. For this you can use system():
system(sprintf("display %s.png", filename))

Gnuplot gprintf() doesn't work anymore on new computer

I worte a script for gnuplot in my university and took the data to my computer at home.
Here is the relevant code:
set terminal postscript eps enhanced color font 'Helvetica,20';
set encoding utf8;
do for[i=2:12] for[j in "3 4 6"]{
set autoscale x;
set xr[-3:3];
set autoscale y;
set key right bottom;
set terminal postscript eps color ;
set termoption dash;
set xlabel "bla";
set ylabel "bla";
set output 'home/Plots/test-'.i.'-'.j.'.eps';
plot 'home/Daten/test-'.i.'-'.j.'.dat' with lines lw 4 title gprintf("homogen q=%.3f",(1/(j-1)-1/(10*(j-1)))/2); }
The gprintf() doesnt work..it only gives me 0.00 but at the computer in my uni it does it right...help anyone :(
edit:
I found out that gnuplot rounds...when i type in
gprintf("homogen q=%.3f",j)
it gives me q=3 for j=3.00
If i use:
gprintf("homogen q=%.3f",j/5)
it gives me q=0.00
it has to be a problem with gnuplot..because, as i said, on the other computer it worked :/
Answer
Use
... gprintf("homogen q=%.3f",(1.0/(j-1.0)-1.0/(10.0*(j-1.0)))/2.0)
The loop variables are integers by default and gnuplot is quite literal in its use of integer arithmetic, as you have already discovered...
Unrequested Advice
As a matter of style, I'd suggest to simplify the individual line's title to q = ... and use a descriptive plot title, e.g.,
Response curves for different values of the specific energy "q"

Junk in the terminal when trying to export .png

I have a file (called print_1012720.txt) that looks like the text shown below.
1133254688 5698771509078629376
1150031904 5698771509371165696
1150031904 5698771510035551232
4170258464 5698771510036082688
2895583264 5698771510036715520
1620908064 5698771510037202176
346232864 5698771510037665280 <----
3366459424 5698771510038193664
2091784224 5698771510332259072
817109024 5698771510332816128 <-----
3837335584 5698771510333344512
2562660384 5698771510339882240
1287985184 5698771510340411392
13309984 5698771510340939776 <-------
3033536544 5698771510348048896
1758861344 5698771510348577280
484186144 5698771510349228800
3504412704 5698771510632804864
2229737504 5698771510633441792
955062304 5698771510634390272
3975288864 5698771510638858496
2700613664 5698771510639347712
1425938464 5698771510642663168
134486304 5698771510643387136
3154712864 5698771510643808768
I am running the following commands in bash.
gnuplot
reset
set terminal png
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set xlabel "time"
set ylabel "highest seq number"
set yrange [0:65535]
set title "seq number over time"
set key reverse Left outside
set grid
set style data linespoints
plot "print_1012720" using 1:2 title "Flow 1"
It gives garbage in my shell. I suspect that it is the spacing between the columns that is causing this. However if I correct the spacing then the file is correctly plotted. Any clue how this can be rectified?
I suspect your main problem is that you are not setting an output. After your set terminal command you should set an output file, otherwise gnuplot will spit out the .png to your terminal, which looks like gibberish. Try putting the following in a file (saved as 'plot.plt' for instance) and then run gnuplot plot.plt at the bash command line.
set terminal png
set output 'output.png'
set xdata time
set timefmt "%s"
set format x "%H:%M"
set xlabel "time"
set ylabel "highest seq number"
set title "seq number over time"
set key reverse Left outside
set grid
set style data linespoints
plot "print_1012720.txt" using 1:2 title "Flow 1"
Creating a plot file this way will save you a lot of time typing at the gnuplot command line.
A couple of other comments:
1) You set your yrange to be 0:65535 when your y values from the datafile are much larger.
2) It looks like you are trying to extract a time in UNIX format from the first column in your datafile. For this you would want the line set timefmt '%s' as I have above. However, the time data in the data file appear to be all over the place, as do the y data.
3) You do not need the reset command at the beginning, since running gnuplot this way will create a new instance of gnuplot, so there will be nothing to reset.
4) You also had 'print_1012720' without the '.txt' extension; I'm not sure this was an error on your part.

Gnuplot script, for loop within or adding to existing plot

I would like to plot a series of vertical lines in gnuplot at a set interval.
Some information about the plot.
The plot is mainly some data from a .dat file. The gnuplot script is called by a bash scripts which alters the gnu plot script using sed. This is a snipit of the an old bash script (ugly I'm sure).
sed -i 's/C = CONCEHOLD/C = '${$CO}'/g' $GNUPLOTROOT/plotviscosity.plt
gnuplot $GNUPLOTROOT/plotviscosity.plt
mv my-plot.ps $VISCPLOTNAME
sed -i 's/C = '${$CO}'/C = CONCEHOLD/g' $GNUPLOTROOT/plotviscosity.plt
with the . plt file looking like this.
set title "Viscosity vs Time, C = CONCEHOLD, beta = RATHOLD, zeta = ZETAHOLD"
set xlabel "Time"
set ylabel "Viscosity"
plot "viscout.dat" using 3:2 title 'Viscosity'
# Saving to my-plot.ps
load save.plt
#
I would like to add to this plot a series of vertical lines at a set repeating interval. I have found how to plot vertical lines via http://t16web.lanl.gov/Kawano/gnuplot/parametric-e.html
set parametric
const=3
set trange [1:4]
set xrange [0:5]
set yrange [0:5]
plot const,t
I would like to have
const=repititionperiod*i
where i is an integer belonging to (1,calculateduppedlimit].
I could input repititionperiod via sed again and in a similar vain calculateduppedlimit but need some sort of for loop either within gnuplot or a separate gnuplot script that adds a vertical line to the already created plot within a for loop in my bash script.
I can't find any information on loops within gnu plot or adding to a previously created plot.
Any advice gratefully received.
EDIT: Gnuplot does now in fact now support a for loop, you can read about it here
As I understand gnuplot doesn't have a for loop, although you can generate one of sorts as follows:
Make a file "loop.gp" containing
const = const + 1
#... some gnuplot commands ...
if(const<100) reread
then in a gnuplot terminal, or script write,
const = 3; load "loop.gp";
This will give you a simple loop.
(this example is taken from the misc. section of http://t16web.lanl.gov/Kawano/gnuplot/index-e.html)
For your particular answer you might try adding arrows rather than paremetric lines,
eg.
set arrow from const,1 to const,4 nohead
will do much the same thing.
In this case you loop.gp could be
const = const + repititionperiod
#... some gnuplot commands ...
set arrow from const,1 to const,4 nohead
if(const<calculatedupperlimit) reread
and you would run you loop with
const = 1; repititionperiod=2;calculatedupperlimit = 10; load "loop.gp"; replot;
The replot plots the arrows.
If you "just" want the lines and nothing else - then you will need to feed a graph to actually plot (a set of arrows doesn't count). The example you gave could then be used to plot the first vertical line.
hope this helps.
Tom

Resources