Running Gnuplot as a live graph, with automatic updates

I am working on some GPU-based software whose output I would like to visualise with a graph or two, and thought Gnuplot would do the job. Being able to watch the graph change as the program ran would be much better than plotting after all the data to be produced. Unfortunately, Gnuplot cannot be fed data from its standard-in, only commands.

Gnuplot Live Update

After a bit of looking around I found a Perl script that allows live updating. However, this is a bit more heavy-duty than I would like. Indeed, there is a much simpler way of getting live updates on a graph.

Gnuplot has some useful commands we can use:

These are fairly self-explanatory, so let’s make a Gnuplot file, liveplot.gnu, that refreshes itself once every second.

We set the bounds of our graph, then plot the data from the file. using 1:2  means plot columns 1 and 2 as x and y, respectively. with lines  means that the points are joined together rather than plotted separately. We pause for 1 second and then reread, meaning that the command file is re-executed.

To test this, we can write to the file plot.dat  over time and watch the graph update live. Here is a sample bash script to do that:

The function writedata  writes the data points for a basic quadratic graph, y=x^2, where the x and y values are separated. The function sleeps for a second after writing every line. The function writedata  is executed in the background and the Gnuplot script we wrote above is launched.

When this bash script is launched in the same directory as liveplot.gnu as above, the graph can be seen to be generated in real time.