Plotting multiple files every file in separate graph using gnuplot - gnuplot

I have more than 1000 files named as "snap%d_beta800.dat" where %d is a number between 1 and 1000.
I want to plot every one of these files in a separate surface plot (splot function) (using three columns) , save the result in png format with the same name as the original file: e.g snap1.png
I want to write a script that can do this for all the 1000 files in just once by loading a gpl file
In addition to that i want to create an animation for the 1000 files.
I am appreciating if you can help with that and please have a look of what I tried
what I tried does not give me a separate plot for every file, it just accumulates the plots of all the files in only one plot
set term png
splot [][][-3:3] for [i=1:1000] 'snap'.i.'_beta800.dat' us\
($1)-($4)/2:($2)-($5)/2:($3*0)-($6)/2:\
($4)*1:($5)*1:($6):($6) w vec head filled size screen 0.015,10,30 lw 2 lc pal z
set output "snap".i.".png"
replot
set term x11

As #GRSousaJr wrote, put it into a do for loop.
I'm wondering why you are writing your plot command like this:
... using ($1)-($4)/2:($2)-($5)/2:($3*0)-($6)/2:($4)*1:($5)*1:($6):($6) ...
I would simply write:
... using ($1-$4/2):($2-$5/2):(-$6/2):4:5:6:6 ...
Code:
### Batch create PNG files
set term pngcairo size 600,600
do for [i=1:1000] {
fname_in = sprintf("snap%d_beta800.dat",i)
fname_out = sprintf("snap%d_beta800.png",i)
set output fname_out
splot fname_in u ($1-$4/2):($2-$5/2):(-$6/2):4:5:6:6 \
w vec head filled size screen 0.015,10,30 lw 2 lc pal z
}
set output
### end of code
I assume you want create your animation from these 1000 PNG files with some other software. Maybe you are aware that you can also create an animated GIF with gnuplot:
Code:
### Create animated file
set term gif size 600,600 animate delay 12 loop 0 optimize
set output "Animation.gif"
do for [i=1:1000] {
fname = sprintf("snap%d_beta800.dat",i)
splot fname u ($1-$4/2):($2-$5/2):(-$6/2):4:5:6:6 \
w vec head filled size screen 0.015,10,30 lw 2 lc pal z
}
set output
### end of code

Related

Plotting constant from a file in GNUplot

I have a data file containing a gaussian function, and an other date file that contains one column with 3 rows. Those three row are all constant which are
1: mean+variance
2: mean
3: mean-variance
from the gaussian in the first file.
I would like to plot all these as constant lines on the gaussian function. I've tried the "every" command, (plot "stat.dat" every ::0::0 w lines) which didn't work.
Thank you, any help is appreciated.
Do you mean something like this?
set terminal pngcairo
set output "gauss.png"
set samples 1000
x0 = -5
s2 = 1
set xrange [-10:10]
set yrange [0:0.5]
plot (1/sqrt(2*pi*s2))*exp(-(x-x0)**2/(2*s2)) title "Gaussian", \
"stat.dat" u 1:(5) every ::0::0 w impulse title "mean + variance", \
"stat.dat" u 1:(5) every ::1::1 w impulse title "mean", \
"stat.dat" u 1:(5) every ::2::2 w impulse title "mean - variance"
I have replaced your data file which contains the gaussian function by an analytical expression. The result looks as follows:

Gnuplot 3d time animation from data file

I realise this is perhaps trivial and if I had more time I'd probably easily deal with it myself, but I'm running out of time and I desperately need to get this animation working as soon as possible.
I have data file of the type
0 28.3976 25.1876 12.7771
0.03125 34.1689 21.457 9.70863
0.0625 35.7084 17.6016 5.03987
0.09375 34.3048 13.6718 1.45238
...
where the first column is meant to be treated as time (it is in fact a numerical solution to a certain ODE system). Now. what I need is an animation of a 3d plot of the last three columns tracing a curve as it moves around with time. Is that doable? If so, how? I'm a complete gnuplot beginner and googling around did not help much. I would hugely appreciate any help. Cheers!
The following should show you an animated plot:
# define fixed axis-ranges
set xrange [-1:1]
set yrange [0:20]
set zrange [-1:1]
# filename and n=number of lines of your data
filedata = 'data.dat'
n = system(sprintf('cat %s | wc -l', filedata))
do for [j=1:n] {
set title 'time '.j
splot filedata u 2:3:4 every ::1::j w l lw 2, \
filedata u 2:3:4 every ::j::j w p pt 7 ps 2
}
The first line of the splot command plots the trayectory, and the second line plots the point at the current time.
If you want a gif of this output, simply add the following before the for-loop:
set term gif animate
set output 'output.gif'
This is an example output:
Related:
StackOverflow: Gif Animation in Gnuplot
gnuplot-surprising: creating gif animation
gnuplotting: Animation IV – trajectory

Plot data and fit-functions of multiple files into one plot

I have N input files and I want to plot the data of these files together with their fit function into one single plot (i.e. one plot for all files, data and fit-function).
After a long time of fiddling I found a solution (see below), but I find it "cumbersome and ugly" and I'm wondering if there is a better, more elegant way of achieving the same thing.
I should say that I'm on gnuplot 5.0 under Windows. The test script below doesn't specify a terminal (I'm testing with windows and wxt), but the final script will use pngcairo terminal.
Things that I find sub-optimal about my solution:
I need two intermediary tables $data and $fit. My original attempt was to use a do for{} loop to read each file in turn perform the fit and generate the plot, but that didn't work out.
Rather than using a fit function, I plot the fit curve (in this simple case a straight line) as data into a table. I experimented with creating on-the-fly user functions using eval but just couldn't quite figure it out (especially how to keep them in sync with the data).
I want the fit-equation to be displayed in the chart. I do this by setting labels, but it would be nicer if it would just be part of the key.
My test data:
data1.dat
100 0.15
200 0.29
300 0.46
400 0.58
data2.dat
100 0.12
200 0.22
300 0.35
400 0.48
data3.dat
100 0.1
200 0.22
300 0.29
400 0.40
My gnuplot script:
set key left
set xrange [0:*]
set yrange [0:0.5]
# user function for linear fit
lin(x) = slope * x + offset
max(a,b) = ((a>=b)? a : b)
file_list = "data1 data2 data3"
x_max = 0
# first write all data of interest into a (memory) table
set table $data
do for [name in file_list] {
filename = name . ".dat"
plot filename u 1:2
print ""
print ""
x_max = max(GPVAL_DATA_X_MAX, x_max)
}
unset table
x_max = max(GPVAL_DATA_X_MAX, x_max)
num_indices = words(file_list)
# now calculate a linear fit for each dataset
set sample 2
set table $fit
do for [i = 0:(num_indices-1)]{
fit lin(x) $data index i using 1:2 via slope, offset
plot [0:x_max][0:0.5] lin(x)
set label (i+1) sprintf("%s = %.3g*x + %.3g", word(file_list, i+1)."(x) ", slope, offset) at 200,(0.20 - 0.05*i)
}
unset table
set title "Data and Linear Fit"
set xlabel "x"
set ylabel "y"
#now we got both data and fit for all files, plot everything at once
plot for [i = 0:(num_indices-1)] $data index i title word(file_list,i+1) with points lc i+1, for [i = 0:(num_indices-1)] $fit index i with lines lc i+1 noti
There is always the stupid, brute force way. You can create a new datafile containing all points you want to fit (e.g. using "cat data1.dat data2.dat data3.dat > newdata.dat" in a linux system and then fit newdata).

Gnuplot script will not work but manually inputting commands works fine

When I plot something manually it works fine, however when I put it into a script it gives an error.
I have data files named g000000, g000007, g000008, g000010...etc corresponding to different time-steps of a simulation.
Each data file is an ascii file with many columns of data. I would like to make a plot of x vs y (columns 1 and 2) and use column 17 to color the data.
Here is the code I have:
set term gif size 1000,600
set xr [-0.05:0.05]
set yr [-0.05:0.05]
set xlabel "X"
set ylabel "Y"
filelist = system('ls g000*')
do for [file in filelist] {
set output 'gasFe.'.(file[2:*]).'.gif'
plot file u 1:2:17 w d lc palette title (file)
}
However I get an error saying
;
set output 'gasFe.'.(file[2:*]).'.gif';
plot file u 1:2:17 w d lc palette title (file);
line 11: warning: Skipping data file with no valid points
;
set output 'gasFe.'.(file[2:*]).'.gif';
plot file u 1:2:17 w d lc palette title (file);
line 11: warning: Skipping data file with no valid points
...etc
However doing this manually using the same commands above without using a script works fine! So what is the problem???

how to output dots/arrows at the ends of line

I am new to gnuplot, I need to plot my data and display a small circle or an arrow at each end of the line chart. how can I do that?
I use this command to display the line chart:
plot 'data.txt' with lines
I don't know if there is a way to make lines have something at the end automatically, but I found a workaround. With this data file:
1 1
2 3
3 2
and the following script:
set term png
set out 'plot.png'
stats 'data.dat' name 'a'
# plot line, then circle only if it is the last data point
plot 'data.dat' t 'data', \
'' u ($0==(a_records-1)?$1:1/0):2 with points pt 7 ps 2 t 'end'
I can make a plot like this:
The stats command is to find the number of data points, then the dual plot command draws the line connecting the data points, then a circle only on the last data point (determined with the a_records variable. An arrow would be trickier to draw...
To find more info about different point/line style options, the test command at the gnuplot command line is your friend.

Resources