Gnuplot:未定义/缺少数据点和绘图样式’带图像’

我必须创建一个彩色地图,而“带图像”的绘图风格正是我想要的. (在x,y位置绘制z的确切值,所以使用pm3d不是我的选项)

问题是,我的数据文件中有未定义的点.例如,函数表示质量比,因此负z值没有物理意义,我想省略它们.或者一些z值甚至是“NaN”.

示例数据文件:

1.0   1.0    1.5
1.0   2.0    1.7
1.0   3.0    1.9
2.0   1.0    1.6
2.0   2.0    1.8
2.0   3.0    2.0
3.0   1.0    1.7
3.0   2.0    1.9
3.0   3.0   -1.0

所以我不想在位置(3,3)处绘制值-1,而是将(3,3)处的像素留空.

我试过这个:

plot './test.dat' u 1:2:($3>0 ? $3 : 1/0) with image

但它不起作用.它说:

warning: Number of pixels cannot be factored into integers matching grid. N = 8 K = 3

set datafile missing "NaN"

在-1.0被“NaN”替换的情况下也不起作用.

我找到的唯一选择是:

set pointsize 10
plot './test.dat' u 1:2:($3>0 ? $3 : 1/0) palette pt 5

但是我必须手动调整每个绘图的点数,x和y范围以及绘图大小,以便数据点没有空格或重叠. (见this question.)

所以简而言之:有没有办法使用带有未定义/缺失数据点的“带图像”绘图样式并将这些点留白?

最佳答案 在这种情况下,我还没有找到一种让gnuplot很好地处理NaN的方法.它为我设置为1,这似乎很奇怪,但可能是“绘图…与图像”处理缺失数据的一个特征.

如果您只想消除负数,可以使用一个技巧:

#!/usr/bin/env gnuplot

set terminal png 
set output 'test.png'

filter(x) = (x > 0) ? x : 1/0 
philter(x) = (x > 0) ? x : 0 

# just in case
set zero 1e-20

# make points set to zero be white
set palette defined (0 1.0 1.0 1.0, \
                 1e-19 0.0 0.0 1.0, \
                     1 1.0 0.0 0.0)

# get min/max for setting color range
stats 'test.dat' u (filter($3)) nooutput

# set color range so minimum value is not plotted as white
set cbrange [STATS_min*(1-1e-6):STATS_max]

plot './test.dat' u 1:2:(philter($3)) with image

在您的数据文件上,它生成此图:

它不是很理想,因为颜色条的底部有白色位,它不能处理NaN.不可能摆脱白色位的原因是,在设置调色板时,使用的数字只是自动调整以适应任何颜色条,并且调色板中有一些离散数量的插槽(256?).因此,调色板中的第一个插槽将始终显示调色板开始的值(白色),无论调色板中的下一个颜色是否显示通过刻度的方式的1e-19.

点赞