From: Farhad Yahyaie on
Hi,

I have a bunch of data and I want to plot the Histogram graph of them. However, I need to have the percentage of the number of data with respect to the total number of them, instead of just the number of data located at each interval. For example if
X = [1 1 2 3 4]
I want the value of the first interval to be 40 (%) instead of 2; that is 2/5 * 100
I used:
tem_store = hist(x)
and I tried to use "Bar" instead of "Hist"; but in that case the intervals are not positioned like the Histogram and bins are separated.

I would be very thankful if any one can help me out.

Regards,
Farhad.
From: ImageAnalyst on
Farhad
Like any normalization, you just divide by the sum of the values.
And use the 'barwidth' property to set the width of the bars. 1 =
touching.

clc;
close all;
workspace; % Display workspace panel.

x = 4 * rand(1, 1000);
subplot(2,2,1);
plot(x);
xlabel('Index');
ylabel('Input Value');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

numberOfBins = 16;
[counts, binValues] = hist(x, numberOfBins);
subplot(2,2,2);
bar(binValues, counts, 'barwidth', 1);
xlabel('Input Value');
ylabel('Absolute Count');

normalizedCounts = 100 * counts / sum(counts);
subplot(2,1,2);
bar(binValues, normalizedCounts, 'barwidth', 1);
xlabel('Input Value');
ylabel('Normalized Count [%]');

From: Farhad Yahyaie on
Hi,

Thanks a lot for your comprehensive reply. I really like your example.

Cheers,
Farhad.

ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <4a861baa-2f5d-4542-82a7-470ba2e9eb70(a)b9g2000yqd.googlegroups.com>...
> Farhad
> Like any normalization, you just divide by the sum of the values.
> And use the 'barwidth' property to set the width of the bars. 1 =
> touching.
>
> clc;
> close all;
> workspace; % Display workspace panel.
>
> x = 4 * rand(1, 1000);
> subplot(2,2,1);
> plot(x);
> xlabel('Index');
> ylabel('Input Value');
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
>
> numberOfBins = 16;
> [counts, binValues] = hist(x, numberOfBins);
> subplot(2,2,2);
> bar(binValues, counts, 'barwidth', 1);
> xlabel('Input Value');
> ylabel('Absolute Count');
>
> normalizedCounts = 100 * counts / sum(counts);
> subplot(2,1,2);
> bar(binValues, normalizedCounts, 'barwidth', 1);
> xlabel('Input Value');
> ylabel('Normalized Count [%]');
From: ImageAnalyst on
You're welcome.
Set the YTickLabel property of the axes if you want the "%" symbol to
appear next to each number along the Y axis, rather than just on the Y
title.