Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Perhaps there are better solutions out there but I can come out with one here. Essentially, I'm creating an RGB representation of the image to show it. So I picked a colormap with the number of colors I wanted at first. Then, from the data, convert them to index and use <code>in2rgb()</code> to get the RGB image. Then, I can get the bin size of each color and label the color bar appropriately.<img src="https://i.stack.imgur.com/9T1k8.png" alt="An example plot"></p> <pre><code>% Randomly generate some data for demonstration A = [1 nan nan; 1 2 3; 4 5 6]; B = [nan 0 nan; 4 3 2; 1 2 1]; C = (A - B) ./ ((A + B) ./ 2); % Number of colors you want to use ncolor = 8; data_colormap = [jet(ncolor); 1 1 1; 0 0 0; 0.5 0.5 0.5]; data_range = [min(C(:)) max(C(:))]; data_ind = (C - data_range(1)) / (data_range(2) - data_range(1)) * (ncolor - 1) + 1; % Assign indices &gt; ncolor for the special cases data_ind(isnan(A)) = ncolor + 1; % isnan(A) assign to white data_ind(isnan(B)) = ncolor + 2; % isnan(A) assign to black data_ind(isnan(A) &amp; isnan(B)) = ncolor + 3; % isnan(A) + isnan(B) % Get the RGB representation img = ind2rgb(round(data_ind), data_colormap); imagesc(img) % Custom labels for the colorbar bin_size = (data_range(2)-data_range(1)) / ncolor; caxis([data_range(1) data_range(2) + 3*bin_size]) colormap(data_colormap) ax = colorbar; yticks = get(ax, 'YTick'); yticks = yticks(yticks &lt; data_range(2)); yticklabels = num2cell(yticks); % Pad another 3 custom labels yticks = [yticks, data_range(2)+[bin_size 2*bin_size 3*bin_size]-0.5*bin_size]; yticklabels = [yticklabels 'isnan(A)', 'isnan(B)', 'both']; set(ax, 'YTick', yticks, 'YTickLabel', yticklabels) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload