# How to fix the values of different histograms 2D to have the same color reference?

10 views (last 30 days)
Jose L. Santacruz on 10 Jan 2019
Commented: Image Analyst on 11 Jan 2019
Hi, I'm trynig to plot several histograms and I'll like to see the difference between them. For that, Matlab fixes the colors based on the maximum value for each histogram, so despite of having different values (amplitude) between histograms the color of the highest and the lowest are the same. That makes it difficult to have an idea of the real difference between them, and makes it necessary to check the colorbar. In the images, I would like to have more intense colors for the first histogram and less intense for the second one, since the values are higher in the first one. How could I fix this?
Here is the code I'm using for the histograms:
string1 = 'HL vs MML at';
string2 = num2str(currentFreq);
ti = strcat (string1,{' '},string2,{' '},'Hz');
Xedges = [0 0:10:100 100];
Yedges = [0 0:10:100 100];
h = histogram2(A, B,Xedges,Yedges,'DisplayStyle','tile','ShowEmptyBins','on');
hcb=colorbar;
colorTitleHandle = get(hcb,'Title');
titleString = 'Number of ears';
set(colorTitleHandle ,'String',titleString);
ylabel('MML (dB)')
xlabel('Hearing Loss (dB HL)')

Image Analyst on 11 Jan 2019
You can specify caxis() for each plot to be the same.
% First do one image.
% then call caxis():
caxis([0, 33]);
% Now make second image in second axes.
% And apply the same caxis limits.
caxis([0, 33]);

#### 1 Comment

Jose L. Santacruz on 11 Jan 2019
Perfect, thanks a lot!

Shubham Gupta on 11 Jan 2019
Edited: Shubham Gupta on 11 Jan 2019
One way to do this is to use "colormapeditor". Type the following in command window :
colormapeditor
In the dialog box, you can change the limits of the Colorbar to be equal for both plots.
Note : Setting colormap limits to be the maximum of the two plots should work much better

Jose L. Santacruz on 11 Jan 2019
Thanks for that tip!
Image Analyst on 11 Jan 2019
"Note : Setting colormap limits to be the maximum of the two plots should work much better"
To do this, you use caxis() like I showed in my Answer.