Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Percentage display on Y-axis of Histogram

1,671 views
Skip to first unread message

Farhad Yahyaie

unread,
Jan 24, 2010, 1:12:02 AM1/24/10
to
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.

ImageAnalyst

unread,
Jan 24, 2010, 9:54:41 AM1/24/10
to
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 [%]');

Farhad Yahyaie

unread,
Jan 24, 2010, 12:31:02 PM1/24/10
to
Hi,

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

Cheers,
Farhad.

ImageAnalyst <imagea...@mailinator.com> wrote in message <4a861baa-2f5d-4542...@b9g2000yqd.googlegroups.com>...

ImageAnalyst

unread,
Jan 24, 2010, 12:44:10 PM1/24/10
to
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.
0 new messages