Creating an animation from multiple data files - gnuplot

I have some C++ code that generates data I want to create an animated gif (or equivalent) from. The data is output into .txt files with the names 1, 2, 3, 4 ..., N with 2 columns (x y data points). For simplicity say we use 100 files.
There seems to be 1 of 2 ways to do this, either create 100 png images from the 100 files then use GIMP to create a gif or create a gif automatically via GNUplot. The first I should be able to do with a loop, say;
set term png
for [i=1:100] {set output "data".i."png"; plot 'filepath/'.i.'.txt' with lines title ""; set output}
Which gives me the error: 'invalid complex constant'. This I suspect is just be being bad with GNUplot syntax.
As for the second, the examples I can find make it unclear how to use data to generate the plots.
Any help is much appreciated.

For gnuplot versions older than 4.6 you can use reread to do such kind of looping.
Consider the file looper.gp:
set output 'data'.i.'.png'
plot 'filepath/'.i.'.txt' with lines notitle
i = i + 1
if (i <= 100) reread
Call this with
i = 1
set terminal png
load 'looper.gp'
set output

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?

Separate file plots from different data files gnuplot

I have 3000 .dat files which I want to plot using gnuplot.
They are all named as "iteration_1", ...", iteration_93", ..."iteration_1247",... (not in the format "iteration_XXXX", if this information is helpful).
Each of those files is to be plotted in an .eps file - my final intention is to do a video (an evolution of those plots), which I can easily make if I have the .eps files.
Is there any way to quickly command gnuplot to do this? All the questions I have found remotely similar to my situation were all actually regarding putting data from different files into one plot in a single file.
Again, I do not want to put all the plots into a single .eps file. I want 3000 .eps files.
Thanks in advance!
Simply put your plotting routine in a do for loop. By the way, gnuplot can also do animated GIFs. Check help gif.
### create output files in a loop
reset session
set terminal epscairo
do for [i=1:3000] {
FILE = sprintf("iteration_%d",i)
set output FILE.".eps"
plot FILE.".dat" u 1:2 w l # or change your extension and plot command accordingly
}
set output
### end of code

"pixel dimension information" when plotting with image in gnuplot

Using gnuplot v5 patch 6 on windows 10 (wxt terminal)
I have a data file of 2D vectors arranged in six columns (x, y, v_x, v_y, v_mag, rho) that I'm trying to plot as a heatmap of v_mag against x and y. The plot generates fine, but it's always coming up with
"No dimension information for 80000 pixels total. Try 200 x 400"
But I have no idea where to specify this in the terminal.
I realise that I can use pm3d map for this, but this doesn't work without setting dgrid3d and that causes problems with plotting dots on top of the heatmap which I'm also doing. I also don't want to generate a matrix file just for the image plot since I need the vector data for analysis later.
In terms of an example, the plot will generate if I literally just write:
plot 'vectors.dat' using 1:2:5 with image, 'dots.dat' with dots
EDIT: added 'set pm3d' to example code
EDIT: example is now minimal code to produce desired plot
EDIT: example data file can be found here
Any help would be appreciated.
After updating my gnuplot to v5.2, this problem has disappeared.

gnuplot - multiple plot files using the same style settings, but different number of plot lines?

This is my problem: I have 4 different data files; and I need to create various plots on png, using data in these files.
I would like to have all in a function that I call in a script, so I would like to put together as many common statement as I can.
The plot have different file names, but they use mostly the same settings: the legend position, the title, the axis label, the range, border line style.
What change is the data, coming from different data files, the number of lines on the same plot (some has for example 1 set of data, others has 4-5 per plot), and the color to differentiate them.
Is there a clean way to group what is similar, so I don't end up writing the same things for each of my plot? I've check the doc and I was not able to find a solution to this; since the style is set for each dataset, so I can't really do anything to group it.
Found some questions here that look similar, but the problem is totally different...I don't need to merge data from different dataset, but I need to create different plot files, which only share most of the common settings. To make a generic example, I need a way to do something like a CSS style file, so the style stay the same, but the content of the plot (and the name of the file) changes.
I am using shell script for the code; so I wrapped a gnuplot command in a shell function.
Thanks
You can put all common settings in one file (lets say settings.gp) and load them from your main files with load 'settings.gp'. That works as if you would write the actual commands in place of the load command. Therefore you can define some variables before loading the settings file to change the behavior.
File settings.gp:
set terminal pngcairo
set output outfile
set style increment user
if (plotNum == 2) {
set style line 1 lt 5
set style line 2 lt 6
} else {
set for [i=1:5] style line i lt i+2
}
(Note, that this kind of if statement requires gnuplot version 4.6 and newer).
File main.gp
outfile = 'first.png'
plotNum = 2
load 'settings.gp'
plot x, x**2
The command set style increment user automatically iterates over line styles instead of line types in the plot command.
That is of course only an example, basically you can include any kind of tests and conditions in you settings.gp. An other possiblity is using the call command.

how to make several plots from the same standard input data in gnuplot?

I want to have a single .plt file storing both data and gnuplot commands. My data looks like
# x1 y1 x2 y2
1 2 3 4
5 6 7 8
and corresponds to two plots: (x1,y1) and (x2,y2).
I know I can use "-" like:
plot "-" using 1:2
# x1 y1 x2 y2
1 2 3 4
5 6 7 8
e
But that would generate only one plot, i.e., (x1,y1). I'm trying to do something like
plot "-" using 1:2, "-" using 3:4
# x1 y1 x2 y2
1 2 3 4
5 6 7 8
e
but obviously that doesn't work since gnuplot expects a new set of data from the standard input for the second "-".
Notes:
I cannot change the style of the data. It comes in four columns.
It seems that I can do it with reread but that requires two files. I really want only one file.
You can't do this without modifying something about the way you input the data. When feeding gnuplot data via standard input, it expects multiple data sets to be delimited with two blank lines between them, or to be interleaved on successive lines. The options are:
Feed the two data sets into different
plot commands altogether.
Change the
file format so that data sets have
blank lines between them, then
reference them all with index.
Change the file format so that
alternating lines represent different
data sets, then reference them all
with every.
Put the data into one
file, the plotting script into
another, and then reference the data
file more than once with different
using clauses each time.
There's an intro to the every and index commands starting at How do I plot several data sets in a single file? Those are the only facilities built into gnuplot for this sort of thing, and neither does exactly what you were asking about. it's good you've already modified the data formatting, because this wasn't ever going to work as you'd originally hoped.
I'm not sure how much you can edit the file, but the tidiest way is probably to put the whole thing in a shell script/batch script (are you on linux or windows?)
On linux I do something like this
#!/bin/bash
#put my data in a file
echo "
# x1 y1 x2 y2
1 2 3 4
5 6 7 8
" > my_dat.dat
#launch gnuplot
gnuplot<<EOF
#gnuplot commands here
set output "test.ps"
set term postscript
plot "my_dat.dat" u 1:2, \
"my_dat.dat" u 3:4
set term pop
set output
EOF
# cleanup
rm my_dat.dat
Then I chmod +wrx the file I put the above commands in and run.
Note: there also seems to be a similarity to this question:
gnuplot stdin, how to plot two lines?
So you might want to look there too
New option since gp5.0 (see also help inline data) :
$dataset << EOD
1 2 3 4
5 6 7 8
EOD
plot $dataset using 1:2, $dataset using 3:4
I know this is an old post, but I would like to point to another strategy in case someone else still struggles with this issue:
You could also use your plotting command and input the data twice, like:
plot "-" using 1:2, "-" using 3:4
# 1 2 3 4
5 6 7 8
e
1 2 3 4
5 6 7 8
e
Gnuplot will actually wait for two blocks in this case. I find this very useful when I don't want to change the commands and when I am feeding Gnuplot by pipe. In a real-time scenario (depending on the data-size) this is very likely to be still faster than buffering to a file on the hard-drive.
In my experience the amount of code required to buffer the data in your script, which is required to pipe it more than once, is very low.

Resources