title: Overlaying data on an image in matlab
body: <p>I have two plots I want to overlay on top of each other as shown in the link below:</p>
  
  <p><img src="https://i.stack.imgur.com/sSL39.jpg" alt="enter image description here"></p>
  
  <p>The inputs are the two images on the left and the output is the image on the right. Here's the code I used:</p>
  
  <pre><code>reference = imread('ref_foam.png');
  figure, imshow(reference);
  hold on;
  h = imshow(data,[]);
  hold off
  colormap jet;
  alphamap = zeros(size(reference,1),size(reference,2));
  for i = 0:size(data,1)-1
      for j = 0:size(data,2)-1
          if(~(data(i+1,j+1) == 0))
               alphamap(i+1,j+1) = 0.75;
          end            
      end
  end
  set(h, 'AlphaData', alphamap);
  </code></pre>
  
  <p>Anytime there's a zero in the data array, it sets that transparency to zero or else it will set the transparency to 0.75.</p>
  
  <p>Now, my questions are: How can I get the colormap to apply to only the data array? In this example it works but if I convert "reference" to a grayscale, the colormap applies to that as well. The input for the colormap is an axes handle, is there anyway to input the image's handle (h) so that it only applies to the top (data) array? Also, I'd like to implement a colorbar as well. Is there anyway to apply the colorbar only to the data array? Thanks.</p>