# How to find probability density for two data sets plotted on same histogram?

1 vue (au cours des 30 derniers jours)
ishita agrawal le 15 Sep 2017
Commenté : ishita agrawal le 16 Sep 2017
I want to plot two data sets on one histogram. Also, I want to show probability density at y-axis. I am using this code,
BinEdges = [0:0.25:16.5]; histogram(data1,'Normalization','probability','BinEdges',edges1,'FaceColor','b','FaceAlpha',0.7)
hold on
histogram(data2,'Normalization','probability','BinEdges',edges1,'FaceColor','r','FaceAlpha',0.7)
The problem is that this code is normalizing both plots individually. Is there a way to plot these two datasets with same probability density scale and also in different colors. I have attached data.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Josh Meyer le 15 Sep 2017
Modifié(e) : Josh Meyer le 15 Sep 2017
Two ideas come to mind...
1. You can combine the data sets into one so that the normalization takes into account the total number of elements. But the tradeoff is that the data is plotted as a single histogram.
edges1 = [0:0.25:16.5];
histogram([data1;data2],'Normalization','probability','BinEdges',edges1,'FaceColor','b','FaceAlpha',0.7)
2. You can manually compute the bin counts and normalize over both data sets, then feed the bins to histogram. This allows you to keep separate histograms with different colors, but histogram just does the plotting and you need to do the calculations.
edges1 = [0:0.25:16.5];
counts1 = ...
counts2 = ...
histogram('BinEdges',edges1,'BinCounts',counts1,'FaceColor','b','FaceAlpha',0.7)
hold on
histogram('BinEdges',edges1,'BinCounts',counts2,'FaceColor','r','FaceAlpha',0.7)
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
ishita agrawal le 16 Sep 2017
Than you for your answer. I can not use option one since I have to plot them in different colors. I'll try second option.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Histograms dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by