I'm trying to plot data as it arrives in realtime with gnuplot.

I've arrived at a fairly simple perl solution, but it involves writing the data to a temporary file and then replotting the whole file every second.

I'm afraid this will result in too much overhead towards the end of the day, as the number of data points accumulates.

I have a perl implementation of my approach, and I'd like suggestions from any gnuplot gurus out there as to more efficient ways to do this.

I've looked at Chart::Graph::Gnuplot, and looked at a few gnuplot related nodes here, but I don't think they solves this problem.

Here's a dummy data source. In reality, this could take input from anything that outputs a time and a value.

#!/usr/bin/perl -w use strict; use POSIX qw(strftime); my $value=rand(100); $|=1; while(1) { sleep(1); print strftime("%H:%M:%S",localtime),",$value\n"; # jitter the value $value+=rand(2)-1; }
Here's the actual gnuplot-calling code. This assumes gnuplot is on your path, and that you have File::Temp installed.

If you don't have File::Temp, it should be simple to adjust the script to use a fixed file name.

#!/usr/bin/perl -w use strict; use File::Temp qw(tempfile tempdir); use IO::Handle; my $tempdir=tempdir(CLEANUP=>1); my ($plotFh, $plotFilename)=tempfile(DIR=>$tempdir); # replace this with whatever the real data src is if needed open(DATASRC,"perl dataSrc.pl|") or die "Can't run data source, error +$!"; open(GNUPLOT,"|gnuplot"); $plotFh->autoflush(1); select GNUPLOT; $|=1; # set up plot options for time series graph print GNUPLOT "set xdata time\n"; print GNUPLOT q(set timefmt "%H:%M:%S"),qq(\n); my $prevTime=""; my $prevValue=0; while(<DATASRC>) { chomp; my ($time, $value)=split /,/; if($time ne $prevTime) { if($prevTime ne "") { print $plotFh "$prevTime $prevValue\n"; print GNUPLOT "plot '$plotFilename' using 1:2\n"; } $prevTime=$time; } $prevValue=$value; }
Any ideas?

TIA!
--
Mike


In reply to Running (real-)time series plots with gnuplot? by RMGir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.