You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Two histograms with different x axis /values
23 views (last 30 days)
Show older comments
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..
Accepted Answer
Voss
on 11 Feb 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 Comments
Turbulence Analysis
on 11 Feb 2022
I tried like this, however, I planning to use tow different x scale, so that both histograms looks bigger...
Image Analyst
on 11 Feb 2022
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.
Image Analyst
on 11 Feb 2022
If we're done here, maybe you can "Accept this answer" to give (no name) his/her reputation points for this answer.
Turbulence Analysis
on 12 Feb 2022
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
on 12 Feb 2022
Edited: Voss
on 12 Feb 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
on 12 Feb 2022
Edited: Image Analyst
on 12 Feb 2022
Boy, how confusing is that to the viewer? Not sure why you want that. Anyway, some questions remain:
- 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?
- Do you want both histograms to have the same number of bins so that the bars overlap (except that the heights may differ)?
- 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?
- And I believe you'd need a legend, right?
Turbulence Analysis
on 12 Feb 2022
- 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..
- 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..
- 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)
- And I believe you'd need a legend, right?
Yes, I need legend too.
Voss
on 12 Feb 2022
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]);

Turbulence Analysis
on 14 Feb 2022
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
Turbulence Analysis
on 2 Mar 2022
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)
Voss
on 2 Mar 2022
% 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]);

More Answers (1)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia-Pacífico
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)