Iterating over powers of two in gnuplot - gnuplot

I want to plot the data stored in bunch of files using gnuplot. If the files were named using sequential numbers, (eg. "1.dat" "2.dat", ...) I'd use something like
plot for [i=1:10] i.'.dat' u 1:2 w lp t 'I='.i;
However, the files are now named using powers of 2, i.e. "2.dat", "4.dat", "8.dat", .... I tried
plot for [i=1:10] (2**i).'.dat' u 1:2 w lp t 'I='.(2**i);
but I get the error
STRING operator applied to non-STRING type
I suppose this happens because gnuplot considers (2**i) as a floating point number rather than integer.
I'm sure there is a way to do what I want to do but as I'm very new to using the control statements of gnuplot I cannot find out how. Could someone please help me?

You can use sprintf to convert a number to a string:
plot for [i=1:10] sprintf('%d',2**i).'.dat' u 1:2 w lp t 'I='.(2**i)
Interestingly, concatenating (2**i) with 'I=' in the title causes no problems.

Try using an empty string ("") to commence the string concatenation operation. That is "".(2**i).".dat" instead of (2**i).".dat".

Related

Is using a gnuplot variable in an external datafile manipulation command possible?

Using an external datafile manipulation cmd in gnuplot looks like
plot '<(grep "1" "/path/datafile")' using 1:2 ...
Can we use a gnuplot variable in such a cmd like in
plot for [i=1:5] '<(grep "i" "/path/datafile")' using 1:2 ...
where "i" should be the gnuplot variable i.
Is it possible to hand over the gnuplot variable to the external command and how?
Thx!
You can build a string command using the concatenation operator ".". Eg
plot for [i=1:5] "<(grep '".i."' /path/datafile)" using 1:2 ...
You might want to grep for something less ambiguous than single digits like 1 that are likely to match within any number. For example, add word delimiters like \b, or even just a leading space. This is why I added the single quoting of the value as a preliminary step (we are generating grep '1' ...).

How to reverse the order of plots or "plot for [n:-1:1] ..."

I am plotting as follows:
plot for [i=1:n] word(fnames,i) ... with lines ,\
'otherfile.csv' ... with linepoints
I need otherfile.csv to be on top of word(fnames,n), on top of word(fnames,n-1), ... on top of word(fnames,1), but the resulting order is just the other way around.
So I am looking for a way of either reversing the order of lines being drawn or an expression like [i=n:-1:1] both of which I was unable to find.
Many thanks in advance!
Update
I accepted Erics answer below, but I will rather use [i=n:1:-1] as suggested by maij in a comment below for this approach does not require me to adapt the arguments of word occurrences in the plot command.
What about?
plot 'otherfile.csv' ... with linepoints,\
for [i=1:n] word(fnames,n-i+1) ... with lines

Gnuplot: Change Density of dotted line in splot

I am trying to plot a dotted line within an splot with the following code in Gnuplot 4.6 patchlevel 4:
set terminal "pdfcairo" enhanced dashed size 15,10
set pm3d map
set output "test.pdf"
splot 'map.dat' using 1:($2/1000):3 notitle, \
'line1.dat' using 1:($2/1000):1 notitle with lines ls 2, \
'line2.dat' using 1:($2/1000):1 notitle with lines ls 2
unset output
The heat map works and so does line1.dat. However, the second line appears mostly solid. The difference is that line1.dat has 70 entries and line2.dat has 900. The second line has a jump between two points and there it is dotted.
Does somebody know how I can change the dot density so that the whole line appears dotted. Changing the original data file is not an option.
Thank you for your help,
noes
EDIT:
One workaround I found is
splot 'line2.dat' every ...
but that can get unconvenient at the jump in the data.
The command (s)plot 'line.dat' with lines first plots the datapoints and then connects the datapoints using lines with the respective linestyle. If the datapoints are too close to each other, there is no place for some gaps when a dashed linestyle is used.
To display a dotted/dashed line, you can try to replace the points by a function or to reduce the number of points.
Try dotted lines instead of dashed lines. Linestyle and linecolor can be set independently: splot 'line.dat' with lines ls 0 lc 2. 900 points might be too many for this approach.
Fitting a function would work, but probably it is too difficult to find a suitable function.
The every option reduces the number of points.
Another possibility to reduce the number of points would be to interpolate the points using the smooth option. This requires a temporary file and works as follows:
# [prepare plot]
set samples 100
set table "line2.dat.tmp"
plot 'line2.dat' using 1:($2/1000) smooth mcsplines with lines ls 2
unset table
set terminal "pdfcairo" enhanced dashed size 15,10
set pm3d map
set output "test.pdf"
# [plot]
splot 'map.dat' using 1:($2/1000):3 notitle, \
'line1.dat' using 1:($2/1000):1 notitle with lines ls 2, \
'line2.dat.tmp' using 1:2:1 notitle with lines ls 2
unset output
In the [prepare plot] section a temporary file "line2.dat.tmp" is created which contains datapoints interpolating line2.dat. You have to play with set samples to get the right number of points. In the example we have 100 equidistant points instead of 900 points with different distances.
The option smooth mcsplines preserves the monotonicity and convexity of the original data points, see help smooth mcsplines in a gnuplot shell.
In the [plot] section the original "lines2.dat" is replaced by the interpolated data.
This approach works if the original data is smooth enough so that replacing 900 points by 100 points does not skip important information. Maybe you want to plot both "lines2.dat" and "lines2.dat.tmp" in a single diagram to compare them.
User the every key-word, like this:
'line2.dat' every 20 using 1:($2/1000):1 notitle with lines ls 2

set multiple delimiter with gnuplot

I'm doing some simulations in OpenFOAM, using probes to get a time series of the velocity in a point. The output file has the following delimiter setup.
if it is possible, what would be the command to set the delimiter ?? when using gnuplot
set datafile separator '???'
Bonus info if I remove all ( and ) and use the default command the plot "plots"
plot "U" using 1:2
You can use
plot "U" using 1:2 "%lf (%lf %lf %lf)"
This format specifies your data format in the plot command. See help using for more details on this and the using examples for more complex examples.
If you don't want to type this each time, and you have a copy of gnuplot compiled with support for string macros, you also can do
dformat = "\"%lf (%lf %lf %lf)\""
plot "U" using 1:2 #dformat
which will expand the format specifier into the command. See help macros for more on this.
I would suggest to use sed :
plot "< sed 's|[()]||g' U" u 1:2

Gnuplot: plotting from string skips the first line

Here's my minimal Gnuplot script:
data="3.000000\t49.200000\n3.500000\t42.800000\n4.000000\t37.800000\n4.500000\t33.800000\n5.000000\t30.400000\n5.500000\t28.000000\n"
plot '< echo -e '.sprintf('"%s"', data) using 1:2 title 'there is no data point for x=3.0?' w linespoints
In my actual script, of course, I populate the data string in a different way (using the stats command), so saving the data to a file first, then running plot should work, but I don't like it! Seems overly cumbersome, leaves stray files around, etc.
My current solution is to prepend the string with a dummy line (data="0\t0\n..."), but my concern is: am I doing something wrong, or is this a bug?
(I'm on ubuntu 14.04, gnuplot 4.6 patchlevel 4, which I guess is not the super-most-up-to-date...)
Thanks!
Remove the -e option and it works fine:
data="3.000000\t49.200000\n3.500000\t42.800000\n4.000000\t37.800000\n4.500000\t33.800000\n5.000000\t30.400000\n5.500000\t28.000000\n"
plot '< echo '.sprintf('"%s"', data) using 1:2 title 'there is a data point for x=3.0!' w linespoints
But I can't tell you exactly why it works ;)
As an outlook for you: Gnuplot 5 has a new way of saving inline data as some kind of heredoc. In my eyes it isn't a good way to include actual data files into the plotting script, but it is supported:
$data <<EOD
3.000000 49.200000
3.500000 42.800000
4.000000 37.800000
4.500000 33.800000
5.000000 30.400000
5.500000 28.000000
EOD
plot $data using 1:2 notitle w linespoints

Resources