我试图(经常,例如每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-安定-plot.股票
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
。确保您位于同一目录中。