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. :-)
You can achieve that by giving the respective pixel a value of 'NaN':
Note, that this depends on the selected terminal. It works at least with
wxt
,pdfcairo
,pngcairo
andpng
, and does not work withx11
andpostscript
. 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:
The result with 4.6.5 is:
Using your example data file, the following script works fine and gives a nice result: