Search code examples
matlabcolorsbar-chartvisualizationmatlab-figure

Painting all bars less lower than a threshold with a single color


I have a bivariate histogram plot created using bar3. I'm trying to change the color of the bars that have a height less than a certain threshold, but to no avail. I got this code:

h = bar3(dataSample, 0.5);
for n=1:numel(h)
     cdata=get(h(n),'zdata');
     set(h(n),'cdata',cdata,'facecolor','interp')
end

Current result

I can't figure out how to make the plot look like the one below, where the bars less than say 0.001 are gray:

Desired result

Any ideas?


Solution

  • here's how:

    z=peaks(20);
    h=bar3(z) 
    for n=1:numel(h)
     cdata=get(h(n),'zdata');
     set(h(n),'cdata',cdata,'facecolor','interp')
    end
    
    colormap([0.5.*ones(128,3); parula(128)]);
    

    enter image description here

    I've arbitrarily decided to cut the colormap in the middle, first 128 intensities as gray the next 128 intensities in color. you can cut it however you want. You can find the threshold you want by setting the colormap binning (say to 256 bins) and the place in that partition below which it'll be gray.