Two histograms with different x axis /values

Hi,
I am trying plot histogram with two different x values on the single plot. Did somebody tried something similar like this before..
For example, as shown in the attached files, data in the file A spans from 0 to 100, and whereas data in file B spans over 0 to 2500..

1 comentario

It's not clear how the contents of the attached .mat file relate to the data in file A and file B as described.
load('matlab.mat');
whos
Name Size Bytes Class Attributes ans 1x35 70 char data 4650x4 148800 double
data(1:10,:)
ans = 10×4
-79.0908 131.4540 -0.3971 -0.7163 -76.9090 131.4540 -0.3315 -0.5731 -74.7272 131.4540 -0.2215 -0.3192 -72.5453 131.4540 -0.1336 0.0441 -70.3635 131.4540 -0.0808 0.1728 -68.1817 131.4540 -0.0107 0.2184 -65.9999 131.4540 0.0603 0.0918 -63.8181 131.4540 0.2081 0.0550 -61.6363 131.4540 0.3676 -0.0100 -59.4544 131.4540 0.4558 -0.0613
[min(data); max(data)]
ans = 2×4
-79.0908 -1.6367 -3.5377 -2.9382 82.3636 131.4540 4.3992 13.6677

Iniciar sesión para comentar.

 Respuesta aceptada

Voss
Voss el 11 de Feb. de 2022
% some data:
A = 12*randn(1,100)+50;
B = 300*randn(1,1000)+1250;
% some histograms from the data:
histogram(B);
hold on
histogram(A);

17 comentarios

I tried like this, however, I planning to use tow different x scale, so that both histograms looks bigger...
OK so go ahead. Since the x axis is the values axis, just rescale them both to have the same max
% some data:
A = 12*randn(1,100)+50;
B = 300*randn(1,1000)+1250;
maxA = max(A(:))
maxA = 83.7912
maxB = max(B(:))
maxB = 2.1703e+03
maxValue = max([maxA, maxB])
maxValue = 2.1703e+03
% Scale data
A = A * maxValue / maxA;
B = B * maxValue / maxB;
% Plot histograms from the data:
histogram(B);
hold on
histogram(A);
grid on;
If you want, you can specify the edges of the bins or number of bins so that the bin widths overlap.
Thanks a lot, Image analyst..
Image Analyst
Image Analyst el 11 de Feb. de 2022
If we're done here, maybe you can "Accept this answer" to give (no name) his/her reputation points for this answer.
Sorry forgot... Done !!
Hi, IM.
Actually, is there a way to have two axis in the histogram without scaling the values. Like what we usually have in the plots (e.g. multi y /x axis)
Voss
Voss el 12 de Feb. de 2022
Editada: Voss el 12 de Feb. de 2022
Like this?
% some data:
A = 12*randn(1,100)+50;
B = 300*randn(1,1000)+1250;
figure();
ax_b = gca();
ax_a = copyobj(ax_b,gcf());
colors = get(ax_a,'ColorOrder');
% some histograms from the data:
histogram(ax_a,A,'FaceColor',colors(2,:),'FaceAlpha',0.5);
histogram(ax_b,B,'FaceColor',colors(1,:),'FaceAlpha',0.5);
set(ax_a, ...
'XGrid','on', ...
'YGrid','on', ...
'Box','off', ...
'Color','none', ...
'XAxisLocation','top', ...
'YAxisLocation','right', ...
'XColor',colors(2,:), ...
'YColor',colors(2,:));
set(ax_b, ...
'XGrid','on', ...
'YGrid','on', ...
'Box','off', ...
'XColor',colors(1,:), ...
'YColor',colors(1,:));
Image Analyst
Image Analyst el 12 de Feb. de 2022
Editada: Image Analyst el 12 de Feb. de 2022
Boy, how confusing is that to the viewer? Not sure why you want that. Anyway, some questions remain:
  1. Do you want both y values to be scaled to a common maximum height? Like each normalized independently like this last plot, or don't scale y and use the actual counts, like I did in my last comment?
  2. Do you want both histograms to have the same number of bins so that the bars overlap (except that the heights may differ)?
  3. Do you want the x axis to have the same values? But what if the data is not in the same range (like 1-100, and 0-2000)? Which x axis values would you want to display - the values from the first histogram or values from the second histogram?
  4. And I believe you'd need a legend, right?
  1. Do you want both y values to be scaled to a common maximum height? Like each normalized independently like this last plot, or don't scale y and use the actual counts, like I did in my last comment?
It depends, but mostly Y axis scale remains same.. Because Y axis represents number density . Yes, it is similar to the one you shown in the previous..
  1. Do you want both histograms to have the same number of bins so that the bars overlap (except that the heights may differ)?
Yes, same no. of bins would be perfect..
  1. Do you want the x axis to have the same values? But what if the data is not in the same range (like 1-100, and 0-2000)? Which x axis values would you want to display - the values from the first histogram or values from the second histogram?
Yes, the data is not in same range. I always need to compare the histograms of two data sets, one's range is 0 to 2000 and other is 0 to 100. Hence, it is better to show x axis in the both the ranges (like shown in the above plot)
  1. And I believe you'd need a legend, right?
Yes, I need legend too.
How about this then?
% some data:
A = 12*randn(1,100)+50;
B = 300*randn(1,1000)+1250;
figure();
ax_b = gca();
ax_a = copyobj(ax_b,gcf());
colors = get(ax_a,'ColorOrder');
n_bins = 20;
% some histograms from the data:
h_a = histogram(ax_a,A,n_bins,'FaceColor',colors(2,:),'FaceAlpha',0.5);
h_b = histogram(ax_b,B,n_bins,'FaceColor',colors(1,:),'FaceAlpha',0.5);
ylabel(ax_a,'A');
ylabel(ax_b,'B');
set(ax_a, ...
'Box','off', ...
'Color','none', ...
'XAxisLocation','top', ...
'YAxisLocation','right', ...
'XColor',colors(2,:), ...
'YColor',colors(2,:));
set(ax_b, ...
'Box','off', ...
'XColor',colors(1,:), ...
'YColor',colors(1,:));
legend([h_a h_b]);
Yes, Thanks a lot.. great!!
Voss
Voss el 12 de Feb. de 2022
Fanftastic!
Hi,
Just one quick follow up question,
I am trying to set xlim for both the x axes, however, I am not succesful with it.. Could you please help me
If you wanted to set the X-Limits of ax_a (red) to [30 80] and ax_b (blue) to [500 2000], you could say:
xlim(ax_a,[30 80]);
xlim(ax_b,[500 2000]);
or:
set(ax_a,'XLim',[30 80]);
set(ax_b,'XLim',[500 2000]);
Hi,
Sorry for the follow up question in this thread, Is it possible to add third histogram C in this plot. Here the x axis ranges of A and C are same ( i.e. 0 to 100)
% some data:
A = 12*randn(1,100)+50;
B = 300*randn(1,1000)+1250;
C = 12*randn(1,100)+50;
figure();
ax_b = gca();
ax_a = copyobj(ax_b,gcf());
colors = get(ax_a,'ColorOrder');
n_bins = 20;
% some histograms from the data:
h_a = histogram(ax_a,A,n_bins,'FaceColor',colors(2,:),'FaceAlpha',0.5);
h_b = histogram(ax_b,B,n_bins,'FaceColor',colors(1,:),'FaceAlpha',0.5);
set(ax_a,'NextPlot','add');
h_c = histogram(ax_a,C,n_bins, ...
'BinEdges',get(h_a,'BinEdges'), ...
'FaceColor',colors(3,:),'FaceAlpha',0.5);
ylabel(ax_a,'A, C');
ylabel(ax_b,'B');
set(ax_a, ...
'Box','off', ...
'Color','none', ...
'XAxisLocation','top', ...
'YAxisLocation','right', ...
'XColor',colors(2,:), ...
'YColor',colors(2,:));
set(ax_b, ...
'Box','off', ...
'XColor',colors(1,:), ...
'YColor',colors(1,:));
legend([h_a h_b h_c]);
Excellent Thanks

Iniciar sesión para comentar.

Preguntada:

el 11 de Feb. de 2022

Comentada:

el 2 de Mzo. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by