How to plot to the same file overwriting it? - gnuplot

I am fiddling with gnuplot. I am not able to output results to the terminal, but that's a different question.
I can plot into a png file. But each time I plot something I need to call set output "plot.png".
Is there a setting that allows to overwrite the existing file?

Related

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

how to save two plots simultaneously in gnuplot

Hiii...
I havebeen trying to plot two curves simultaneously in a single plot to compare them. ie, by the command:
plot "1.txt" w l, "2.txt" w l
now I want to save it, but the usual command for saving is:
set out "1.txt"
but in this case how can I save them together in a same plot?
For saving a plot as a file on your disk, you need to setup two things: a terminal and an output. Suppose you have configured your plot interactively in a command-line gnuplot session, this is an example:
set terminal png
set output "myplot.png"
replot
unset output
where replot does what its command name suggests, it repeats the last plot command - and if that last plot command showed both curves as you wanted it, they will identically show up in the output file. For info on supported terminal types on your system, read help terminal.

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

Set with lines in gnuplot

I can connect the dots of a data file by including with lines in the plot command. Is there a way to set this fixed (I was hoping for set with lines) so I don't have to explicitly include it in every call of plot?
Use
set style data line
to achieve this. Similar options are also available for functions
set style function boxes

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.

Resources