..大家好。
我想绘制矩阵颜色图(热图), 不过,我想使用一行打印的数据来绘制热图。 我试过了,
p for[i=5:15] 'line' u (i%2):(i/2%10):i+1 w image
所以gnuplot显示警告图像至少需要2维数据
我的数据集是这样的
0.1(node 1 value at time step 1) 0.1(node 2 "") 0.3(node 3 "") 0.2(node 4 "")
0.5(node 1 value at time step 2) 1.2(node 2 "") 0.7(node 3 "") 0.2(node 4 "")
0.8(node 1 value at time step 3) 2.2(node 2 "") 0.1(node 3 "") 0.1(node 4 "")
0.1(node 1 value at time step 4) 1.2(node 2 "") 1.1(node 3 "") 0.4(node 4 "")
0.4(node 1 value at time step 5) 1.1(node 2 "") 0.7(node 3 "") 0.6(node 4 "")
0.3(node 1 value at time step 6) 0.4(node 2 "") 0.2(node 3 "") 0.3(node 4 "")
0.2(node 1 value at time step 7) 0.3(node 2 "") 0.7(node 3 "") 0.2(node 4 "")
.
.
.
.
在上面的数据中,一行中有 4 个值。 在一行中使用 4 个值,我想制作 2X2 颜色矩阵 每隔 0.3 秒我就想换行来制作变色视频。 我可以制作视频,但问题是,如何使用一行数据制作矩阵。
而且我不想转换为 x:y:z 数据来绘制图像。 我的数据有点长和重,我担心它会降低速度。 我需要绘制实时热图。 随着值的变化,颜色也会实时变化
我也尝试过调色板,
p for[i=5:15] 'line' u (i%2):(i/2%10):i+1 w p pt 5 ps 23.5 palette
但是,x轴和y轴之间有空白区域(白色)。
所以,看起来不太好。
如何使用一行打印的数据绘制热图?
实际上,如果我可以绘制实时热图而不将数据保存在文件中, 会好起来的。 (在 C 代码中打开 gnuplot 并将值传递给 gnuplot)
提前谢谢
最佳答案
基本上有几种方法可以实现你想要的。首先,我展示了将静态数据处理成动画 gif 的三种可能性:
使用外部工具(此处为
awk
)处理一次数据文件。(1 的变体:使用
awk
等外部工具动态处理数据)仅使用 gnuplot
变体 1:预处理数据
我认为适合您的数据格式是
N1T1 N2T1
N3T1 N4T1
N1T2 N2T2
N3T2 N4T2
...
注意数据集之间的两个换行符。这样您就可以使用 index
关键字来选择不同的数据集。
要将数据预处理到此文件中,请从 gnuplot 调用
system('if [ ! -e dataconv ]; then awk ''{ print $1 " " $2 "\n" $3 " " $4 "\n\n"}'' data > dataconv; fi')
如果您希望所有帧都有固定的颜色范围,您可以使用固定值(如果您知道范围),或从数据文件中提取它们:
stats 'dataconv' using 1:2
timesteps = int(STATS_records/2)
cbmin = (STATS_min_x > STATS_min_y ? STATS_min_y : STATS_min_x)
cbmax = (STATS_max_x > STATS_max_y ? STATS_max_x : STATS_max_y)
set cbrange[cbmin:cbmax]
现在,为了绘制数据,您最终可以使用matrix
并使用index
顺序选择所有时间步:
set terminal gif animate delay 30
set output 'animate.gif'
unset key
set xrange[-0.5:1.5]
set yrange[-0.5:1.5]
set xtics 1
set ytics 1
do for [i=0:(timesteps-1)] {
set title sprintf('time step % 3d', i+1)
plot 'dataconv' matrix index i with image
}
unset output
这里,我只显示第一个时间步的结果:
变体 2:动态处理数据
您可以使用 awk
来选择时间步长并重新格式化数据。为了简单起见,这里我手动设置了固定的颜色范围:
reset
set cbrange[0:3]
set terminal gif animate delay 30
set output 'animate2.gif'
unset key
set xrange[-0.5:1.5]
set yrange[-0.5:1.5]
set xtics 1
set ytics 1
timesteps = int(system('wc -l data | cut -d " " -f1'))
do for [i=0:timesteps] {
set title sprintf('time step % 3d', i)
plot '< awk ''{ if (FNR == '.i.') {print $1 " " $2 "\n" $3 " " $4}}'' data' matrix with image
}
unset output
变体 3:仅限 gnuplot
这仅使用 gnuplot 并且不依赖于外部工具,但是更加乏味,因为它需要对 using
语句进行一些修改。问题是,您有一维数据(只有一行),但想将其绘制为二维,这需要特殊的数据格式才能正常工作。
为了伪造 2D 输入,我让 gnuplot 读取两行。当处理第一行时,gnuplot 会记住第 3 列和第 4 列的值,并在第二行的第 1 列和第 2 列中使用这些值。第 2 行的数据将被丢弃。这有一个小缺点,即除非插入虚拟最后一行,否则无法绘制最后一个时间步。另外,对于 4 列,最大和最小颜色值的估计有点冗长:
stats 'data' using 1:2 prefix 'A_'
stats 'data' using 3:4 prefix 'B_'
timesteps = int(A_records)
max(x, y) = x > y ? x : y
min(x, y) = x > y ? y : x
cbmin = min(min(A_min_x, A_min_y), min(B_min_x, B_min_y))
cbmax = max(max(A_max_x, A_max_y), max(B_max_x, B_max_y))
set cbrange[cbmin:cbmax]
同样,如果您知道可能的颜色范围,则可以跳过这部分的大部分内容。
set terminal gif animate delay 30
set output 'animate3.gif'
unset key
A0 = 0
A1 = 0
set xrange[0.5:2.5]
set yrange[0.5:2.5]
set xtics 1
set ytics 1
do for [i=0:(timesteps-2)] {
set title sprintf('time step % 3d', i)
plot 'data' matrix every :::i::(i+1) \
using (A0 = ($1 == 2 && $2 == i) ? $3 : A0, \
A1 = ($1 == 3 && $2 == i) ? $3 : A1, $1+1):\
($2-i+1):\
($2 == i ? $3 : ($1 == 0 ? A0 : ($1 == 1 ? A1 : $3))) \
with image
}
unset output
我希望有一种方法可以满足您的需求。
实时绘图
实时绘图的一种可能性是有一个循环,在其中绘制数据文件的最后一行。然而,如果您的程序没有一次写入完整的行,那么这对于竞争条件来说并不安全:
set cbrange[0:3]
unset key
set xrange[-0.5:1.5]
set yrange[-0.5:1.5]
set xtics 1
set ytics 1
while (1) {
set title "time ".strftime('%T', time(0))
plot '< tail -1 data | awk ''{print $1 " " $2 "\n" $3 " " $4}'' ' matrix with image
pause 0.1
}
要中断,只需按 Ctrl+C
。
关于matrix - 使用一行的 gnuplot 矩阵或调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18290772/