Normalize y axis in Matlab histogram

7 Ansichten (letzte 30 Tage)
Mons Nasery
Mons Nasery am 1 Okt. 2015
Kommentiert: Steven Lord am 1 Okt. 2015
Hi,
suppose I have this histogram:
histogram([resultCastMatch.jaccard]);
Is this the correct way to normalize y axis (so to show y axix as percentage)?
histogram([resultCastMatch.jaccard]);
ylabels = get(gca, 'YTickLabel');
ylabels = linspace(0,100,length(ylabels));
set(gca,'YTickLabel',ylabels);

Antworten (2)

Geoff Hayes
Geoff Hayes am 1 Okt. 2015
Mons - no, I don't think that is the way to go about converting the y-axis to percentages. Remember, the ticks along the y-axis will just tell you the number of elements that fall into each of your bins. So if you want to convert these ticks (or heights) to percentages, you would have to divide each tick by the total number of elements across all bins. Or, depending upon your version of MATLAB, you may be able to specify the Normalization property to have a value of probability (which would produce the desired result without you having to manipulate the ticks). See http://www.mathworks.com/help/matlab/ref/histogram.html#namevaluepairarguments and look for the section on Normalization.
  2 Kommentare
Mons Nasery
Mons Nasery am 1 Okt. 2015
thanks for your useful link :)
Steven Lord
Steven Lord am 1 Okt. 2015
I second Geoff's suggestion of specifying the 'probability' value for the 'Normalization' argument.

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 1 Okt. 2015
Not quite. You have to specify the YTick values as well as the YTickLabel labels:
figure(1)
h1 = histogram(randi(9, 1, 25))
yt = get(gca, 'YTick'); % Get Y-Tick Values
set(gca, 'YTick',yt, 'YTickLabel',yt*4) % Since Data Vector Is (1x25), Multiply ‘yt’ By 4 To Get Percent
ylabel('Percent')
  2 Kommentare
Mons Nasery
Mons Nasery am 1 Okt. 2015
thanks for your kind answer. So, in my case which the y axis shows the number of users and I have 164 users, is it correct?
figure;
h1 = histogram([resultGenreMatch.jaccard]);
yt = get(gca, 'YTick');
set(gca, 'YTick',yt, 'YTickLabel',yt*(100/164));
Star Strider
Star Strider am 1 Okt. 2015
My pleasure.
That looks correct to me. It should work.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by