title: Transparency for specific values in matrix using Gnuplot while preserving the palette?
body: <p>Here's an interesting problem:</p>
  
  <p>I have a 2D "matrix" of double-precision, binary data I'd like to plot using Gnuplot. This is easily done as follows:</p>
  
  <pre><code>plot "foo.dat" binary array=384x384 format='%double' with image
  </code></pre>
  
  <p>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? </p>
  
  <p>I have looked through the relevant parts of the Gnuplot documentation (<a href="http://www.gnuplot.info/docs_4.4/gnuplot.pdf" rel="nofollow noreferrer">link</a>), and I think I need to use the <code>with rgbalpha</code> style, but I'm not sure how that applies or how to map the transparency correctly. </p>
  
  <p>I have also looked at these examples (<a href="http://gnuplot.sourceforge.net/demo/rgbalpha.html" rel="nofollow noreferrer">link</a>), but I'm not sure how I could apply it. </p>
  
  <p>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). </p>
  
  <p>Thanks in advance! </p>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>I thought I'd give some examples for posterity. In all examples, I use the following preamble:</p>
  
  <pre><code>set title "Basic Plot"   # Or as appropriate
  set size square
  set palette @MATLAB   # see &lt;http://www.gnuplotting.org/matlab-colorbar-with-gnuplot/&gt;
  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
  </code></pre>
  
  <p>My basic command produces the following:</p>
  
  <pre><code>plot "foo.dat" binary array=384x384 format='%double' with image
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/DvfhO.png" alt="Basic Plot"></p>
  
  <p>I also tried what @Christoph suggested: replace values equal to <code>val</code> with <code>NaN</code>:</p>
  
  <pre><code>plot "foo.dat" binary array=384x384 format='%double' \
      using ($1 == val ? NaN : $1) with image
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/8RMjY.png" alt="Plot setting val to NaN"></p>
  
  <p>I tried using <code>rgbalpha</code> in order to truly control transparency, and it produces the following:</p>
  
  <pre><code>plot "foo.dat" binary array=384x384 format='%double' \
      using 1:1:1:($1 == val ? 0 : 255) with rgbalpha
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/gxTuM.png" alt="Plot using rgbalpha"></p>
  
  <p><strong><em>CONCLUSION</em></strong></p>
  
  <p>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 <code>@MATLAB</code> palette as I want it to. </p>
  
  <p>If somebody can cause all entries equal to <code>val</code> to be transparent <em>and</em> get the colormap to work correctly, I'll accept their answer. </p>
  
  <p>***Where to go next****</p>
  
  <p>The Gnuplot Documentation mentions the <code>set datafile missing</code> command (<a href="http://gnuplot.sourceforge.net/docs_4.2/node172.html" rel="nofollow noreferrer">link</a>). This looks promising, but it only seems to work for column-based data -- not binary files. </p>
  
  <p>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:</p>
  
  <pre><code>red(z)   = &lt;stuff&gt;
  green(z) = &lt;stuff&gt;
  blue(z)  = &lt;stuff&gt; 
  plot "foo.dat" binary array=384x384 format='%double' \
      using red($1):green($1):blue($1):($1 == val ? 0 : 255) with rgbalpha
  </code></pre>
  
  <p>Bonus points if those functions somehow reference the current palette, so the specifics of a palette aren't hard-coded into these three functions.  :-)</p>