Normalize y axis in Matlab histogram

7 次查看(过去 30 天)
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);

回答(2 个)

Geoff Hayes
Geoff Hayes 2015-10-1
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 个评论
Steven Lord
Steven Lord 2015-10-1
I second Geoff's suggestion of specifying the 'probability' value for the 'Normalization' argument.

请先登录,再进行评论。


Star Strider
Star Strider 2015-10-1
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 个评论
Mons Nasery
Mons Nasery 2015-10-1
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 2015-10-1
My pleasure.
That looks correct to me. It should work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by