保留调色板时使用Gnuplot矩阵中特定值的透明度? [英] Transparency for specific values in matrix using Gnuplot while preserving the palette?

查看:169
本文介绍了保留调色板时使用Gnuplot矩阵中特定值的透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的问题:

我有一个双精度的二维矩阵",我想使用Gnuplot绘制二进制数据.这很容易做到,如下所示:

I have a 2D "matrix" of double-precision, binary data I'd like to plot using Gnuplot. This is easily done as follows:

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

诀窍是数据的某些区域"具有特殊值,例如1234567890.0.我希望这些元素完全透明,而所有其他矩阵整体都完全不透明.有没有一种方法可以根据数据本身设置透明度渠道?

The trick is that certain "regions" of the data have a special value, say 1234567890.0. I want those elements to be fully transparent, and all other matrix entires to be fully opaque. Is there a way to set the transparency channel based on the data itself?

我已经浏览了Gnuplot文档的相关部分(链接 ),我想我需要使用with rgbalpha样式,但是我不确定该如何应用或如何正确映射透明度.

I have looked through the relevant parts of the Gnuplot documentation (link), and I think I need to use the with rgbalpha style, but I'm not sure how that applies or how to map the transparency correctly.

我也查看了这些示例(链接),但我是不知道如何应用它.

I have also looked at these examples (link), but I'm not sure how I could apply it.

我想知道这是否可以单线完成(如上所述),或者是否需要几条线(例如在二进制矩阵顶部绘制轮廓时).

I wonder if this could be done in a one-liner (as above), or if it would need a couple lines (as when plotting contours on top of a binary matrix).

提前谢谢!

UPDATE

UPDATE

我认为我会举一些后代的例子.在所有示例中,我都使用以下序言:

I thought I'd give some examples for posterity. In all examples, I use the following preamble:

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

我的基本命令产生以下内容:

My basic command produces the following:

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

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

I also tried what @Christoph suggested: replace values equal to val with NaN:

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

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

I tried using rgbalpha in order to truly control transparency, and it produces the following:

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

结论

CONCLUSION

前两种方法产生相似的结果.第一个是不好的,因为它用错误的最大值弄乱了颜色表.第一个和第二个不足之处在于它们实际上并没有达到透明性(Gnuplot中的​​未定义"值不会自动赋予透明性).最后一个很棒,因为它实际上控制透明度,但是它是灰度的,并且不使用我想要的@MATLAB调色板.

The first two methods produce similar results. The first is bad because it messes up the colormap with false maxima. The first and second fall short in that they don't actually achieve transparency ("undefined" values in Gnuplot aren't automatically given transparency). The last is great in that it actually controls transparency, but it is in grayscale and doesn't use the @MATLAB palette as I want it to.

如果有人可以使所有等于val的条目透明化 并使颜色图正常工作,我将接受他们的回答.

If somebody can cause all entries equal to val to be transparent and get the colormap to work correctly, I'll accept their answer.

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

***Where to go next****

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

The Gnuplot Documentation mentions the set datafile missing command (link). This looks promising, but it only seems to work for column-based data -- not binary files.

我想回答我最初的问题的最好方法是通过设置一组函数来模仿rgbalpha方法的每个列"的给定调色板-像这样:

I imagine the best way to answer my original question is by setting up a set of functions to imitate a given palette for each "column" of the rgbalpha method -- something like this:

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

奖金指出了那些函数是否以某种方式引用了当前的调色板,因此调色板的细节并没有硬编码到这三个函数中. :-)

Bonus points if those functions somehow reference the current palette, so the specifics of a palette aren't hard-coded into these three functions. :-)

推荐答案

您可以通过为各个像素赋予'NaN'值来实现:

You can achieve that by giving the respective pixel a value of 'NaN':

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

请注意,这取决于所选的端子.它至少与wxtpdfcairopngcairopng一起使用,并且 not x11postscript一起使用.我没有测试其他终端.

Note, that this depends on the selected terminal. It works at least with wxt, pdfcairo, pngcairo and png, and does not work with x11 and postscript. I didn't test other terminals.

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

Here is a self-contained example. The background color is set to show that the pixel indeed is transparent and not white:

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的结果是:

The result with 4.6.5 is:

使用示例数据文件,以下脚本可以很好地工作并给出不错的结果:

Using your example data file, the following script works fine and gives a nice result:

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

这篇关于保留调色板时使用Gnuplot矩阵中特定值的透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆