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 件のコメント
Voss
2022 年 2 月 11 日
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 件のコメント
Turbulence Analysis
2022 年 2 月 11 日
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.
Turbulence Analysis
2022 年 2 月 11 日
Thanks a lot, Image analyst..
Image Analyst
2022 年 2 月 11 日
If we're done here, maybe you can "Accept this answer" to give (no name) his/her reputation points for this answer.
Turbulence Analysis
2022 年 2 月 11 日
Sorry forgot... Done !!
Turbulence Analysis
2022 年 2 月 12 日
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?
Turbulence Analysis
2022 年 2 月 12 日
- 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
2022 年 2 月 12 日
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
2022 年 2 月 12 日
Yes, Thanks a lot.. great!!
Voss
2022 年 2 月 12 日
Fanftastic!
Turbulence Analysis
2022 年 2 月 14 日
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
Voss
2022 年 2 月 15 日
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]);
Turbulence Analysis
2022 年 3 月 2 日
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]);

Turbulence Analysis
2022 年 3 月 2 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
