Determine min and max in gnuplot - gnuplot

I am new to gnuplot and i am trying to determine the mina nd max from a datafile and afterwards plot the data
So far I have managed to determine the min and max like this:
# Define two helper functions
ismin(x) = (x<min)?min=x:0
ismax(x) = (x>max)?max=x:0
# Initialise the 'global' vars
max=-1e38
min=1e38
plot "Data.txt" u 0:(ismin($3)*ismax($3))
The problem is that I am trying to plot the data using splot, and it's not working.
I am trying this:
splot \
'Data.txt' u 2:1:3 with pm3d t '',\
If I remove the part related to determining the min and max, the splot command works.
Any suggestions?

Look into the stats command:
stats 'datafile' using 3
for example, will get statistics on the 3rd column (z data), and store them in variables (STATS_min and STATS_max may be what you want). To see all the variables created, type
show variables all
after running stats. If you have an older version of gnuplot without stats, you can plot the file without creating an output, and gnuplot automatically defines some DATA_-prefixed variables including a min/max. The stats command saves the trouble of defining a null output to get data before plotting.

Related

Using nested for loop in gnuplot

I am trying to plot a set of graphs to compare the simulation results with the experimental data. The simulation files are in ordered arrangement on 7X7X7 for various parameters. I need to plot all of those files using a nested for loop for each iXjXk file. The files are named thus : fibrilAll_i_j_k.dat
I have already tried some alternatives like using multiple for loop in the same line. But it doesn't seem to work.
set terminal eps size 1200,800
set output "all.eps"
set title "{/*2 Alternative rates}"
set ylabel "{/*2 fibril mass fraction}" offset 1.5,0,0
set xlabel "{/*2 Time(h)}"
set key left top
plot 'experiment.txt' using 1:6 ps 2 pt 5 title "EXP",\
for [i=1:7] for [j=1:7] for [k=1:7] 'fibrilAll'._i_j_k.'.dat' using 1:2 with lines title 'i,j,k'
replot
I get the following error message:
internal error : STRING operator applied to undefined or non-STRING variable
I see a few possible problems.
1) I take it you do not want to plot the same file fibrilAll_i_j_k.dat 343 times.
If the data files are named e.g. fibrilAll_1_5_3.dat then you can construct that name by saying plot ... sprintf("fibrilAll_%d_%d_%d.dat",i,j,k)
2) Probably you want something similar for the titles
3) The replot does not accomplish anything. Did you leave out something?

Gnuplot shell script ROS data from multiple files

I'm trying to plot data from two different rosbag files using gnuplot. I'm trying to automate this as I have quite a few files that will need to be run.
I need to take the first element of the first column of each file and offset the data of the column w.r.t. that (and then divide by 10^9) to get the time in seconds. My issue is my script returns something different when I run it multiple times. It will either return the first, the second, or (occasionally) the third plot command, which is what I'm interested in.
The code I've cobbled together is below:
#!/bin/bash
gnuplot -persist <<-EOFMarker
set autoscale
set datafile separator ","
set key autotitle columnhead
plot "bag1" using (\$1):2 with linespoints
first=GPVAL_DATA_X_MIN
plot "bag2" using (\$1):3 with linespoints
second=GPVAL_DATA_X_MIN
plot "bag1" using ((\$1-first)/10**9):2, "bag2" using ((\$1-second)/10**9):3
EOFMarker
An example of the dataset is:
%time,field.1,field.2,field.3
1.50317427276591E+018,23,64,64
1.50317427281556E+018,232,74,64
1.50317427285689E+018,216,76,64
1.50317427287325E+018,183,85,64
1.50317427292519E+018,165,89,64
1.50317427298662E+018,129,96,64
1.50317427300161E+018,115,101,64
1.50317427309547E+018,102,112,64
And the second input file is:
%time,field.1,field.2,field.3,field.4
1.50317425501603E+018,55,45,229,98755
1.50317425501843E+018,55,45,229,98752
1.5031742550235E+018,51,43,229,98758
1.50317425502979E+018,51,43,229,98761
1.50317425504176E+018,55,41,231,98764
1.50317425504579E+018,55,41,231,98770
1.50317425504728E+018,50,42,232,98773
1.50317425504855E+018,50,42,232,98773
1.50317425505353E+018,55,41,229,98770
1.50317425506442E+018,55,41,229,98770
I've never experienced code where multiple runs produces different results. Can anyone point me in the right direction to fix this mess?
The outputs are the three plots below. No error message is output from the script at any time.
First output:
Third (and desired) output:
I don't know why you are sometimes getting different results; I always get the output of the last plot command. However, you seem to be using the first two plot commands only to get the minimum value of the first column for each of the two files. A better way that doesn't generate any plots would be
set datafile separator ","
set key autotitle columnhead
stats "bag1" using 1
first=STATS_min
stats "bag2" using 1
second=STATS_min
plot "bag1" using (($1-first)/10**9):2, "bag2" using (($1-second)/10**9):3

Setting gnuplot yrange to fit one curve only

I have a Gnuplot script that draws 2 curves from a data file. If I don't specify yrange, Gnuplot sets it so that all the points of both curves fit in the figure. In my case I would like Gnuplot to only care about one of the curve (it does not matter if the other goes out of range, since I'm interested only in the parts of the figure where the second curve gets close to the first one).
I could find out the minimum and maximum values taken by the first curve, and manually set yrange to those values, but my Gnuplot script is intended to run automatically on many data files, producing many figures for which yrange is not the same, so I'm looking for a way to do that automatically.
Thanks
There are different options, depending on the used gnuplot version:
Version 5.0:
The data file which should be excluded from the autoscaling gets a noautoscale parameter:
plot 'first.dat' using 1:2, 'second.dat' using 1:2 noautoscale
Version >= 4.6:
Use the stats command to get the minimum and maximum value of the relavant data file:
stats 'first.dat' using 1:2
set yrange [STATS_min_y:STATS_max_y]
plot 'first.dat' using 1:2, 'second.dat' using 1:2
At least since 4.0:
Use set yrange [] writeback to save the autoscaled ranges from a plot command and set yrange restore to use them for a later plot:
set terminal push
set terminal unknown
set yrange [] writeback
plot 'first.dat' using 1:2
set yrange restore
set terminal pop
plot 'first.dat' using 1:2, 'second.dat' using 1:2

gnuplot - How to extract pm3d interpolated data?

I am trying to smooth some sparse data I have (format x,y,z) in a gnuplot graph. I need to plot using plot and the with image option because files generated with pm3d map are exceedingly oversized. What I like about pm3d map is that I can smooth my data with interpolate in such a way that the interpolated point only considers neighboring points (dgrid3d considers every point in the graph for the smoothing).
Hence what I want to do is set table and then export the pm3d map data with the interpolation set to whatever I desire, then use that data to do a plot ... with image. The problem is that when I do the following:
set table "out.dat"
set pm3d map interpolate 10,10
splot "in.dat"
gnuplot ignores the interpolate option and simply writes to "out.dat" the same info that was written in "in.dat".
Any ideas?
Alternatively, any stand alone code that interpolates 3D data in the same fashion of pm3d would also be useful.
Edit:
Apparently, I am using at work an "oldish" version of gnuplot: gnuplot 4.2 patchlevel 6, which only accepts the dgrid3d behavior as described above. At home, I am using a newer version (gnuplot 4.4 patchlevel 3) which comes with the option set dgrid3d {<rows>{,<cols>}} splines. This does more or less what I want.

gnuplot ignores x and y ranges when using dgrid3d

I have a file with scattered data (points located approximatelly on the vertices of a regular grid): first two columns are the x and y coordinates, then a few more columns with other data that I need to plot. I want to obtain color maps that represent this data, and since points are scattered I'm using dgrid3d to generate a regular grid and have a smoother representation. My problem is that when I set dgrid3d, gnuplot ignores the x and y ranges and plot the grid outside the figure frame. Bellow is a minimal script to reproduce my problem:
set view map
set yrange [0.4:0.8]
set xrange [0.2:0.8]
set pm3d
set style data lines
set dgrid3d 100,100,4
splot "./Terr.dat" using 1:2:(log($6)) pal
The result that I obtain is the following image:
Setting the option clip1in or clip4in of pm3d has no effect. If I unset view so that the result is a 3D surface, it also ignores the x and y ranges. I could easily write an script to pre-process the data and remove the points outside the range I want, but gnuplot should be able to manage this. Any idea?
I'm using gnuplot 4.2 patchlevel 6
Thanks!
I'm not sure that I am able to reproduce your problem, but there are a few funny things with your script. I'm not exactly sure what the line set style data lines is supposed to do in this context as you're plotting with pm3d. I created a simple datafile:
0 1 4
1 0 5
0 0 2
1 1 3
And I plotted it using this script:
set view map
set yrange [0.4:0.8]
set xrange [0.2:0.8]
set dgrid3d 100,100,4
splot 'test.dat' u 1:2:3 w pm3d
And it seemed to "work" (I'm using gnuplot 4.6.0).
There are a few things of note however -- Notice that every point in my original domain was out of the given x and y ranges. Gnuplot still used those points when constructing the surface. This is also demonstrates reasonably nicely what the gnuplot weighting function looks like (although we could do even better by using only 1 point in our data file.)
UPDATE
Between my 2 computers, I have access to gnuplot4.2.6, gnuplot4.3.0, gnuplot4.4.2, gnuplot4.6.0, gnuplot4.6.1 and gnuplot4.7.0. gnuplot4.2.6 is the only version which exhibits the behavior you describe. It looks to me like they changed the behavior of pm3d in the 4.3 CVS branch, but didn't push those changes back into gnuplot4.2. The easy fix is to upgrade to gnuplot4.6 -- I've been using it as my default gnuplot for a few months now and it seems to be pretty stable.

Resources