How to visualize two histograms for comparative analysis?

Basically I have two datasets and I want to perform a comparative analysis by showing how many data fall under a specific range. Any idea as how to do it?
Thanks Alex

3 件のコメント

Pieter VL
Pieter VL 2018 年 12 月 6 日
You can change the transparancy of the bars of the second histogram using the 'FaceAlpha' parameter to create a visible overlay as follows:
hist1 = histogram( ..., 'EdgeColor', 'blue', 'FaceColor', 'blue' );
hold on;
hist2 = histogram( ..., 'EdgeColor', 'green', 'FaceColor', 'green', 'FaceAlpha', 0.2 );
Anupam Sharma
Anupam Sharma 2019 年 12 月 12 日
This is not working in following code. I am using it exactly as mentioned above but it shows the second histogram only. Can you help ?
% MATLAB R2019a
% Setup
N = [1:5 10 20 40];
LB = 0;
UB = 3;
n = 10000;
% Generate random variates
X = LB + (UB - LB)*rand(max(N),n);
Sn = cumsum(X);
mu = 1.5;
sigma = .75;
S_1 = mu + sigma.*randn(n, 1)
hist1= histogram(Sn(1,:),'Normalization','pdf','EdgeColor', 'blue', 'FaceColor', 'blue')
hold on
hist2 = histogram(S_1(:), 'EdgeColor', 'green', 'FaceColor', 'green', 'FaceAlpha', 0.2);
Image Analyst
Image Analyst 2019 年 12 月 12 日
Use subplot to put them into different axes.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2018 年 4 月 10 日

2 投票

The histograms will tell you that - how many counts in each range. Use histogram() for visualization, or histcounts() and bar().

3 件のコメント

Alex
Alex 2018 年 4 月 11 日
Thank you Image Analyst. Indeed, I have two datasets (Nx1). For both the datasets, I want to see the number of counts in a specific range. If I use hold on and plot the histograms, it gets overlapped and it is not easy to visualize the difference.
So, I was wondering if I can introduce some sort of transparency or sort of 3D plot.
the cyclist
the cyclist 2018 年 4 月 11 日

Here is an example of using Image Analyst's idea of using both histcounts and bar:

x = randn(2000,1);
y = 0.1 + randn(2000,1);
binRange = -3:0.5:3;
hcx = histcounts(x,[binRange Inf]);
hcy = histcounts(y,[binRange Inf]);
figure
bar(binRange,[hcx;hcy]')
Alex
Alex 2018 年 4 月 11 日
Thanks a lot "the cyclist".

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2018 年 4 月 10 日

0 投票

As shown in the documentation for the histogram histogram, you can plot two overlapping histograms on one figure like this
x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
If you have the Statistics and Machine Learning Toolbox, you can also use the Kolmogorov-Smirnov test to determine whether the CDFs of the two distributions are statistically different.

4 件のコメント

Alex
Alex 2018 年 4 月 11 日
Thank you cyclist for the answer. Yes, I was using the hold on command. Unfortunately, it is difficult to visualize when I want to check number of counts in a range. So, I wondering if I could introduce any transparency or maybe plot 3D?
Milagre MANHIQUE
Milagre MANHIQUE 2022 年 1 月 28 日
Hallo Matlab community
I would like to plot two histograms from two different datasets on the same graph (overlapping and transparent), but each of the histograms containing the respective fit curve.
I tried to use the histfit command on each dataset but was unsuccessful.
I ask for help please
Image Analyst
Image Analyst 2022 年 1 月 28 日
Try histcounts() and bar()
Milagre MANHIQUE
Milagre MANHIQUE 2022 年 1 月 28 日
Thank you very much, I will proceed as you advise.
I thank you deeply.
Milagre MANHIQUE

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by