使用 Gnuplot 矩阵中特定值的透明度,同时保留调色板?

2023-12-02

这是一个有趣的问题:

我有一个双精度二进制数据的二维“矩阵”,我想使用 Gnuplot 进行绘制。这很容易完成,如下所示:

plot "foo.dat" binary array=384x384 format='%double' with image

诀窍在于数据的某些“区域”具有特殊值,例如 1234567890.0。我希望这些元素完全透明,而所有其他矩阵整体完全不透明。有没有办法根据数据本身设置透明通道?

我浏览了 Gnuplot 文档的相关部分(link),我想我需要使用with rgbalpha样式,但我不确定它如何应用或如何正确映射透明度。

我也看过这些例子(link),但我不确定如何应用它。

我想知道这是否可以在一行中完成(如上所述),或者是否需要几行(如在二进制矩阵顶部绘制轮廓时)。

提前致谢!

UPDATE

我想我应该为后代举一些例子。在所有示例中,我使用以下序言:

set title "Basic Plot"   # Or as appropriate
set size square
set palette @MATLAB   # see <http://www.gnuplotting.org/matlab-colorbar-with-gnuplot/>
set cbrange [-100000:100000]    # This cbrange fits my data well enough
                                # I would LOVE an automated way to do this!
val = 1234567890.0      # For missing data

我的基本命令产生以下结果:

plot "foo.dat" binary array=384x384 format='%double' with image

Basic Plot

我还尝试了@Christoph的建议:替换值等于val with NaN:

plot "foo.dat" binary array=384x384 format='%double' \
    using ($1 == val ? NaN : $1) with image

Plot setting val to NaN

我尝试使用rgbalpha为了真正控制透明度,它会产生以下结果:

plot "foo.dat" binary array=384x384 format='%double' \
    using 1:1:1:($1 == val ? 0 : 255) with rgbalpha

Plot using rgbalpha

结论

前两种方法产生相似的结果。第一个很糟糕,因为它用错误的最大值弄乱了色彩图。第一个和第二个的不足之处在于它们实际上并未实现透明度(Gnuplot 中的“未定义”值不会自动赋予透明度)。最后一个很棒,因为它实际上控制透明度,但它是灰度的并且不使用@MATLAB调色板如我所愿。

如果有人可以使所有条目等于val做到透明and让颜色图正常工作,我会接受他们的答案。

***下一步去哪里****

Gnuplot 文档提到了set datafile missing命令 (link)。这看起来很有希望,但它似乎只适用于基于列的数据——不适用于二进制文件。

我想回答我原来的问题的最好方法是设置一组函数来模拟 rgbalpha 方法的每个“列”的给定调色板 - 如下所示:

red(z)   = <stuff>
green(z) = <stuff>
blue(z)  = <stuff> 
plot "foo.dat" binary array=384x384 format='%double' \
    using red($1):green($1):blue($1):($1 == val ? 0 : 255) with rgbalpha

如果这些函数以某种方式引用当前调色板,那么调色板的细节不会被硬编码到这三个函数中,这是加分项。 :-)


您可以通过为相应像素指定“NaN”值来实现此目的:

value = 123456789.0
plot "foo.dat" binary array=384x384 format='%double' \
     using ($1 == val ? NaN : $1) with image

请注意,这取决于所选的终端。它至少适用于wxt, pdfcairo, pngcairo and png,并且确实not与...一起工作x11 and postscript。我没有测试其他终端。

这是一个独立的示例。背景颜色设置为显示像素确实是透明的而不是白色的:

set xrange [0:1]
set yrange [0:1]
set isosamples 11
set samples 11
plot '++' using 1:2:($1 == 0.5 && $2 == 0.5 ? NaN : $1) with image

4.6.5 的结果是:

enter image description here

使用您的示例数据文件,以下脚本可以正常工作并给出良好的结果:

set terminal pngcairo size 800,800
set output 'foo.png'
set size square

f(x)=1.5-4*abs(x)
set palette model RGB
set palette functions f(gray-0.75),f(gray-0.5),f(gray-0.25)

val = 1234567890.0

set autoscale xfix
set autoscale yfix
set cbrange [-10000:10000]
plot 'foo.dat' binary array=(512,512) format='%double' using ($1 == val ? NaN : $1) with image notitle

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Gnuplot 矩阵中特定值的透明度,同时保留调色板? 的相关文章

随机推荐