我正在尝试(经常,例如每 0.01 秒(在 gnuplot 中重新绘制 2D 数据集,方法是在 python 中生成一个小的(例如 10x10(2D numpy 数组并将其保存到文件中。
运行以下命令几秒钟,然后 gnuplot 停止并显示错误"矩阵的扫描大小为零"。
gnuplot > plot 'testfile.out' matrix w image
gnuplot > while(1) {replot; pause 0.01;}
我怎样才能让 gnuplot 忽略这一点并继续重新绘制数据文件?
编辑:下面的方法适用于随机数生成器,但是当我将其应用于我的实际文件时,"矩阵的扫描大小为零"也会发生同样的事情。可能是Python而不是gnuplot的问题?准确地说,我从下面的答案中运行setup-and-plot.gp
:
set term wxt noraise
plot '<flock testfile.out cat testfile.out' matrix w image
while(1) { pause 0.01; replot; }
然后是以下 Python 代码:
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)
几秒钟后(取决于我设置睡眠时间的时间(,它再次停止,并显示"矩阵扫描大小为零"错误。
我认为你不能。
解决方法是使用文件锁定,例如,下面是使用 Gnuplot 脚本和使用 util-linux
包中的 flock 的随机数生成器的示例:
setup-and-plot.gp
set term wxt noraise
plot '<flock testfile.out cat testfile.out' matrix w image
while(1) { pause 0.01; replot; }
发电机(使用 bash 测试(:
while sleep .01; do
flock testfile.out
sh -c "shuf -i 0-100 -n100 | xargs -n10 > testfile.out"
done
在一个终端中运行生成器,在另一个终端中运行gnuplot setup-and-plot.gp
。 确保您位于同一目录中。