Iteratively generate datablocks in gnuplot - gnuplot

Is it possible to iteratively generate datablocks, where the name of the datablock is build up inside the loop?
Let's assume I have three fruits (in reality there are more):
array namelist[3] = ['apple', 'banana', 'pineapple']
I want to create three datablocks with the names $apple_data, $banana_data and $pineapple_data, so I tried the following:
do for [i=1:|namelist|] {
set table '$'.namelist[i]."_data"
plot ...
unset table
}
Unfortunately, instead of datablocks gnuplot created files with these names in the working directory. I guess gnuplot is checking whether the first character after set table is a $?
My second attempt was to remove the apostrophes around $:
set table $.namelist[i]."_data"
But this raised the weird error "Column number or datablock line expected", pointing at the period right after $.
Any ideas on how to achieve that?
The reason for all this is that I read in the banana/apple data files with a lengthy path, apply some lengthy calculations within using, and reuse these for lots of successive stats and plot commands. I would like to avoid having to hard-code and copy-paste the same long path and the cumbersome using command over and over again.

Not sure if I fully understood your detailed intention.
If you only want to avoid typing (or copy pasting) a lengthy path again and again, simply use variables:
FILE1 = 'C:/Dir1/SubDir1/SubSubDir1/SubSubSubDir1/File1'
FILE2 = 'C:/Dir2/SubDir2/SubSubDir2/SubSubSubDir2/File2'
plot FILE1 u 1:2, FILE2 u 1:2
Anyway, you asked for dynamically generated datablocks. One way which comes to my mind is using evaluate, check help evaluate. Check the example below as a starting point, which can probably be simplified.
Code: (simplified thanks to #Eldrad's comment)
### dynamically generate some datablocks
reset session
myNames = 'apple banana pineapple'
myName(i) = word(myNames,i)
N = words(myNames)
set samples 11
do for [i=1:N] {
eval sprintf('set table $%s_data',myName(i))
plot '+' u 1:(rand(0)) w table
unset table
}
plot for [i=1:N] sprintf('$%s_data',myName(i)) w lp pt 7 ti myName(i)
### end of code
Result:

Related

Putting a value from an input data file into 'Set Label'

I'm plotting an animated surface in gnuplot and want to read in an average or sum of the mapped z values and include this in a label to be printed in the plot, so that I get a running total updated as the GIF progresses. It's probably straightforward, but I'm a "gnu"bie, so to speak, and find this system pretty confusing!
I've tried putting the running sum and average numbers in additional columns ...
splot 'output3.dat' index i:i using 1:2:(column(3), TD1 = strcol(4), TD2 = strcol(5)) with pm3d
but this doesn't plot, and the string variables TD1, TD2 don't seem to exist outside the splot command.
The command you show would indeed set variables TD1 and TD2 globally if you change the order of clauses in the serial evaluation expression (the comma-separated sub-expressions):
splot 'output3.dat' index i:i using 1:2:(TD1 = strcol(4), TD2 = strcol(5), column(3)) with pm3d
However, if the idea is to create a label using set label that will appear as part of the resulting graph, this won't work. The set label command would have to be executed before the splot command, so TD1 and TD2 will not have the correct values yet.
There is an alternative that might serve you better. Instead of trying to put this dynamically evaluate information in a label, put it in the plot title. Unlike a label, the plot title is evaluated after the corresponding plot is generated, so any variables set or updated by that plot will be current. [caveat: this is true for current gnuplot (version 5.4) but was not always true. If you have an older gnuplot version the title is evaluated before the plot rather than after].
Since current gnuplot also allows you to place the individual plot titles somewhere other than in the key proper, you have the same freedom that you would with a label to position the text anywhere on the output page. For example, if you want to sum the values in column 3 of the data file and print the total as part of a title above the resulting plot:
SUM = 0
splot 'foo.dat' using 1:2:(SUM = SUM+column(3), column(3)) with linespoints title 'foo.dat', \
keyentry title = sprintf("Points sum to %g", SUM) at screen 0.5 0.9
I used a separate keyentry clause because this allows to omit the sample line segment that would otherwise be generated, but it would also be possible to make this the title of the plot itself if you want that sample line.

Splot triangles from binary data file

I have a binary files that have binary format='%float32%float32%float32', having D3 points (using 1:2:3). Originally logically points grouped by 3 to form triangles. I want to plot just legs of triangles as a lines using with lines. In ASCII data file I can make so-called datablocks by adding an empty line between triples of lines, but in binary I can't. I want to break a chain of lines at every three points to form at least V-like parts of triangle contours.
Is there an option to make gnuplot to treat a triples of points as separate datablocks?
Is there another option to splot the data as (maybe) a solid triangles?
I'm still not sure whether I'm fully on the right track.
The following code writes your binary data into a table and makes groups of 3 datapoints for a triangle, inserting an empty line and then shifted by one for the next triangle.
So, So from the points p1=f1,f2,f3; p2=f4,f5,f6; p3=f7,f8,f9; ... it creates the triangles p1p2p3p1 p2p3p4p2 p3p4p5p3 (space=empty line).
Certainly, not very (memory) efficient, but maybe this gets closer to your final goal.
Check help pm3d and help hidden3d, where you might get some additional information.
I very much hope that there is a better approach.
Code: (assuming the binary file 'myBinary.bin', not sure whether I decoded your ASCII string correctly)
# plot binary data
reset session
# put binary data into a datablock
set table $Data
plot 'myBinary.bin' u 1:2:3 binary format='%float32%float32%float32' skip=12 w table
unset table
# separate each triangle by an empty line
set print $Data2
do for [i=1:|$Data|-2] {
print $Data[i]
print $Data[i+1]
print $Data[i+2]
print $Data[i]
print "" # insert empty line
}
set print
set pm3d hidden3d
set view 40, 24
splot $Data2 u 1:2:3 w pm3d notitle
### end of code
Result:

gnuplot setting line titles by variables

Iam trying to plot multiple data lines with their titles in the key based on the variable which I am using as the index:
plot for [i=0:10] 'filename' index i u 2:7 w lines lw 2 t ' = '/(0.5*i)
However, it cannot seem to do this for a fractional multiple of i. Is there a way around this other than to set the title for each line separately?
sprintf should provide all the functionality needed, e.g.,
plot for [i=0:10] .... t sprintf(" = %.1f", 0.5*i)
in order to use the value of 0.5*i with 1 decimal digit...

gnuplot error: `Image grid must be at least 2 x 2'

I have a file with 5 columns of data in it. The first column are x indexes, the second are y indexes and the following three contain some data on which I perform some calculations in my gnuplot script as follows:
module(a,b,c) = sqrt(a**2+b**2+c**2);
splot 'myfile' using 1:2:(module($3,$4,$5)) with image
This works fine. However, if I try to plot the log10 of the module like this:
module(a,b,c) = log10(sqrt(a**2+b**2+c**2));
splot 'myfile' using 1:2:(module($3,$4,$5)) with image
it returns a blank plot and the warning of the title of the question.
I think it might be due to the zeros that are in my data, but even when I set an xy range to avoid those, the error persists.

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