present 2 histograms in the same X axis values

Hi,
I'm trying to plot 2 histograms in the same x axis. they suppose to have common peaks and I'd like to present them at the same scale so the peaks will overlap. is theres a way to do it?
this is the result I want to achive, but in here I used histcount and it made some other troubles.

5 Comments

The y axis represents counts so normalize the bin height will deflect the data
Sorry, I'm not clearly understand the point.
As mentioned in the histogram help page, you can switch Y axis with counts <-> PDF by setting 'Normalization' option.
Also, you can change Y axis scale to 'log' by changing the axes handle.
Do these options solve your problem?
Adam Danz
Adam Danz on 26 Mar 2020
Edited: Adam Danz on 26 Mar 2020
The image you shared is not a histogram which makes the goal confusing.
sani
sani on 26 Mar 2020
Edited: sani on 26 Mar 2020
yes this image shows what I want to achive more or less, I used histcounts and then I just plot semilog plot.
my data is channel numbers that I need to bin first and then to plot them. the problem is when I'm using Histogram the peaks that suppose to be in the same location are moving (since one data set is smaller then the other and then it need less binning)
I'd like to make 2 overlay histograms from the 2 data sets and that the peaks will be in the same location (as I showed in the image)
I tried to use normalization, but maybe I'm missing something, this is what I got. I need both of the histograms to contain the same number of bins and that the peaks but when I wried to set the bins number I got a mess

Sign in to comment.

Answers (1)

sani
sani on 26 Mar 2020
Edited: Adam Danz on 27 Mar 2020
I'll try to explain myself better.
I have a detector that products text file of channels, from that data I'd like to create counts Vs. energy spctrum (that is basicly histogram og the data). from the same detector I have 2 data sets, which have the same X axis values but different y valuse (different counts per channel). I want to present them one over the other for comparison. the total channel number is 16384.
what I did that partialy worked: I used histcounts on the data file and then I plot it. the problem is that I lost the energy calibration (lost the real channel of incidence by binning). this is the code of what I did:
[X,edges] = histcounts(Ge_table.energy,16384); %gamma spectrum - single mode (log scale)
Z = histcounts(Ge_energy,edges);
semilogy(X,'r')
hold on
semilogy(Z,'b') %gamma spectrum - coincidence mode (log scale)
xlabel('Energy [keV]')
ylabel('Counts')
I tried also to use histogram but I'm sure Iv'e missed something out, this the code by the advice of Akira Agata:
figure (2)
h1 = histogram(Ge_table.energy);
hold on
h2 = histogram(Ge_energy);
set(gca,'YScale','log')
h1.Normalization = 'probability';
h1.BinWidth = 1;
h2.Normalization = 'probability';
h2.BinWidth = 1;
for some reason when I present it in log scale the shape is not make any sence.

6 Comments

Regarding the figure(2), how about setting the edges, like:
figure(2)
histogram(Ge_table.energy,edges,'Normalization','pdf');
hold on
histogram(Ge_energy,edges,'Normalization','pdf');
set(gca,'YScale','log');
If possible, could you upload your data here so that people here can understand the issue more clearly.
this part still not make sence. when I zoom in I see that the histogram with less data is much higher then it supposed to. I added the data in mat files, thanks
OK, how about adjusting the edge resolution ( = bin width) ?
The following is an example:
load('Ge_energy.mat');
load('Ge_table_energy.mat');
% Set edge resolution (bin width) to 250
edges = 0:250:33000;
figure
histogram(Ge_energy,edges,'Normalization','pdf')
hold on
histogram(Ge_table_e,edges,'Normalization','pdf')
ax = gca;
ax.YScale = 'log';
I think there is a problem with the 'normalization' in this case, since (at least for my understanding) it gives the probability density. the real data suppose to look like the first image I posted:
y axis need to represent the real counts per channel or else it deflect the data.
the problem in the case in this image is that I have to change the number of bins for each measurment since despite the detector have 16384 channels, it is not necessary that all of them will collect data- so fixed number of bins not working. (in this case I used histcounts and semilog plot)
It's not clear for me what should be done.
Previously, you said that "need both of the histograms to contain the same number of bins and that the peaks but when I wried to set the bins number I got a mess". So I adjusted edge resolution ( = bin width) and (I think) achieved "both of the histograms to contain the same number of bins" avoiding to "got a mess (figure)".
Or, you mean you want plot only details of the histogram around Energy of ~500 [keV] ?
I'm also struggling a bit to understand the end goal but from what I understand, you have two data sets, one with n values and the other with m values (n ~= m) and you'd like the histogram bins to match. Is that correct?
If so, try this:
combinedData = [data1(:); data2(:)];
edges = linspace(min(combinedData), max(combinedData), 20); % the 20 is the number of bins
histogram(data1, edges);
hold on
histogram(data2, edges);

Sign in to comment.

Categories

Asked:

on 25 Mar 2020

Commented:

on 27 Mar 2020

Community Treasure Hunt

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

Start Hunting!