Two histograms with different x axis /values
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
채택된 답변
Voss
2022년 2월 11일
% 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
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..
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)
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
2022년 2월 12일
편집: Image Analyst
2022년 2월 12일
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?
- 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.
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
2022년 2월 12일
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
추가 답변 (1개)
Turbulence Analysis
2022년 3월 2일
Excellent Thanks
카테고리
도움말 센터 및 File Exchange에서 Histograms에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
