Transparency for specific values in matrix using Gnuplot while preserving the palette?

隐身守侯 提交于 2020-02-15 10:21:54

问题


Here's an interesting problem:

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

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?

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).

Thanks in advance!

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

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

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

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.

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****

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.

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. :-)


回答1:


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

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

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



来源:https://stackoverflow.com/questions/25321213/transparency-for-specific-values-in-matrix-using-gnuplot-while-preserving-the-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!