Command and Script to Re-Read a File in Gnuplot

Command and Script to re-read a file in gnuplot

Change your command to the following:

gnuplot -persist -e "plot 'data.dat' using 1:2 with lines ,'' using 1:3 with lines" loop.plt

This plots columns 1 and 2 and columns 1 and 3 using lines

how to read a script (which is stored as a file) in gnuplot

To run a script called 'filename', you can type

gnuplot "filename"

or from within an interactive gnuplot environment you can load a script file by typing

load "filename"

Edit: As per Gabriel's comment below, you can let the plot generated by a script persist and close your gnuplot session by using the -p or --persist option. Do note that the plot which persists won't have any interactive features like zooming/scrolling.

gnuplot -p "filename"

More info available on the gnuplot official documentation(page 22)

How to automatically replot a graph every few seconds in gnuplot?

Make a script with a loop:

while (1) {
plot "a.dat"
pause 1 # waiting time in seconds
}

Execute it with gnuplot script.gp.


For purposes of code structure and debugging, you might prefer the following alternative:

plot "a.dat"

while (1) {
replot
pause 1
}

This has the advantage that you do not have to put a complicated plot command inside the loop and do not suffer from incorrect line numbers for the plot command in error messages (that happen in at least some version of Gnuplot).


Finally, if your Gnuplot is so old that it does not yet support loops, there is the alternative:

plot "a.dat"

pause 1
reread

With reread making the script interpreter jump to the beginning of the file again.

Parse and plot data received from log-file on-the-fly

Here are two ways for plotting data on-the-fly.

Looping with gnuplot

You must call plot over and over again, the data is preprocessed by an external script. The minimal gnuplot script filter.gp is:

while (1) {
plot '< ./myscript.pl' using 2:1
pause 1
}

To stop this, hit Ctrl+C.

The Perl script for the preprocessing may look like the following myscript.pl:

#!/usr/bin/perl
use strict;
use warnings;

my $path = "file.log";
my @grepped;
my $t = 0;
open(INFILE,"< $path") or die "$! \n";

while (my $line = <INFILE>) {
if ($line =~ m{^Time = (\d+)}){
$t = $1;
};

if ($line =~ m{^Errors: local = (\d+), global = (\d+)}){
print "$t\t$1\t$2\n";
};
};
close(INFILE);

Just run it with gnuplot filter.gp.

To make it more configurable, one can change the script to use a variable which is passed to gnuplot via the command line:

while (1) {
plot '< ./myscript.pl -f '.path using 2:1
pause 1
}

or use reread for this:

plot '< ./myscript.pl -f '.path using 2:1
pause 1
reread

Call this script with gnuplot -e "path='file.log';" filtermod.gp.

That works, but would filters the complete file over and over again.

Piping from Perl to gnuplot

Here is a Perl script, which basically works for me, but its my first real Perl script, so there may be some nonideal parts. Feel free to comment on that.

#!/usr/bin/perl
use strict;
use warnings;

my $path = "file.log";
my @grepped;
my $switch = "off";

open(my $gp, "| gnuplot -persist") or die "$! \n";
$gp->autoflush(0);

use File::Tail;
my $file = File::Tail->new(name=>$path, maxinterval=>1, tail=>-1);

while (defined(my $line= $file->read)) {
if ($line =~ m{^Time = (\d+)}){
push(@grepped,"$1\t");
};

if ($line =~ m{^Errors: local = (\d+), global = (\d+)}){
push(@grepped,"$1\t");
push(@grepped,"$2\n");
$switch = "refresh";
};

if ($switch eq "refresh") {
print $gp <<"GNU_EOF";
plot '-' using 2:1
@grepped
e
GNU_EOF
$gp->flush;
$switch = "off";
};
Time::HiRes::sleep(0.1);
};

What I found to be important here is

  1. Looping over a changing file.
  2. The arrangement of the autoflush and flush.
  3. The sleep to allow gnuplot to process the data properly.

That worked for a very small test data file. Don't know if it will for a larger one as well, but should help you a bit further.

Ubuntu - gnuplot reread deselecting text

You can use a different terminal (pdf, eps, etc.) and keep the output open in evince, which refreshes automatically. This worked for me when I used to work in Ubuntu.

set term pdfcairo
set output 'dynamics.pdf'
...

As for why this is happening: my thought is that if you are running a very new version of Ubuntu, X may not be playing nice with Mir. Otherwise it's not very clear to me; it may be worth filing as a bug report.

Keep Gnuplot auto refresh window inactive

This depends on what terminal you're using. wxt, x11 and qt support a noraise option:

set term x11 1 noraise

This should allow it to stay in the background.

Reading a data file with multiple blocks as one coherent data file

A little cheat with awk:

plot "<awk -F'[ ]' '/\\S/ {printf(\"%f %f\\n\",$1,$2)}' <data.dat "  w l

will ignore blank lines. (\S <=> 'non-whitespace')

NOTE

-F'[ ]' and printf are overinsurances:

plot "<awk  '/\\S/ {print $0}' <data.dat "  w l

is almost as good as.



Related Topics



Leave a reply



Submit