title: Setting Transparency Based on Pixel Values in Matplotlib
body: <p>I am attempting to use matplotlib to plot some figures for a paper I am working on. I have two sets of data in 2D numpy arrays: An ascii hillshade raster which I can happily plot and tweak using:</p>
  
  <pre><code>import matplotlib.pyplot as pp
  import numpy as np
  
  hillshade = np.genfromtxt('hs.asc', delimiter=' ', skip_header=6)[:,:-1]
  
  pp.imshow(hillshade, vmin=0, vmax=255)
  pp.gray()
  pp.show()
  </code></pre>
  
  <p>Which gives:</p>
  
  <p><img src="https://i.stack.imgur.com/mBowA.png" alt="Hillshade"></p>
  
  <p>And a second ascii raster which delineates properties of a river flowing across the landscape. This data can be plotted in the same manner as above, however values in the array which do not correspond to the river network are assigned a no data value of -9999. The aim is to have the no data values set to be transparent so the river values overlie the hillshade.</p>
  
  <p>This is the river data, ideally every pixel represented here as 0 would be completely transparent.</p>
  
  <p><img src="https://i.stack.imgur.com/XkhHC.png" alt="River data"></p>
  
  <p>Having done some research on this it seems I may be able to convert my data into an RGBA array and set the alpha values to only make the unwanted cells transparent. However, the values in the river array are floats and cannot be transformed (as the original values are the whole point of the figure) and I believe the <code>imshow</code> function can only take unsigned integers if using the RGBA format.</p>
  
  <p>Is there any way around this limitation? I had hoped I could simply create a tuple with the pixel value and the alpha value and plot them like that, but this does not seem possible.</p>
  
  <p>I have also had a play with <code>PIL</code> to attempt to create a PNG file of the river data with the no data value transparent, however this seems to automatically scale the pixel values to 0-255, thereby losing the values I need to preserve.</p>
  
  <p>I would welcome any insight anyone has on this problem.</p>