2

I am trying to (frequently, e.g. every 0.01s) replot a 2D dataset in gnuplot, by generating a small (e.g. 10x10) 2D numpy array in python and saving it to a file.

Running the following works for a couple of seconds and then gnuplot stops with the error, "Scan size of matrix is zero".

gnuplot > plot 'testfile.out' matrix w image
gnuplot > while(1) {replot; pause 0.01;}

How can I get gnuplot to ignore this and continue replotting the data file?


Edit: The method below works fine for the random number generator, but when I apply it to my actual file the same thing happens with "scan size of matrix is zero". Possibly it's an issue with Python rather than gnuplot? To be precise, I am running setup-and-plot.gp from the answer below:

    set term wxt noraise
    plot '<flock testfile.out cat testfile.out' matrix w image
    while(1) { pause 0.01; replot; }

and then the following Python code:

    import time
    import numpy as np

    while True:
        # in actual code array is not random, this is just to debug.
        arr = np.random.rand(10,10)
        np.savetxt('testfile.out', arr)
        time.sleep(.01)

After a few seconds (depending on how long I set the sleep time) it stops again, with the "scan size of matrix is zero" error.

1 Answer 1

2

I don't think you can.

A workaround is to use file-locking, e.g. below is an example using a Gnuplot script and a random number generator using flock from the util-linux package:

setup-and-plot.gp

set term wxt noraise
plot '<flock testfile.out cat testfile.out' matrix w image
while(1) { pause 0.01; replot; }

The generator (tested with bash):

while sleep .01; do 
  flock testfile.out \
    sh -c "shuf -i 0-100 -n100 | xargs -n10 > testfile.out"
done

Run the generator in one terminal and gnuplot setup-and-plot.gp in another. Make sure you are in the same directory.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works for your example, but strangely doesn't seem to when I generate the file using python. I've edited my original question; am I missing something obvious?
@user366202: You need to lock the file before writing it. Open the file with open and use fcntl.flock on it before writing (numpy.savetxt() support writing to file descriptors). Remember to release the lock after writing. Example 5 on this page shows the mechanism

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.