Change histogram y-axis to percentage [R2017b]

Good morning,
I have used the histogram function to get a plot of a histogram. Specifically, for presentation, I would actually only like to have the probability value as a percentage plotted against the bins. For this I have used the histogram properties 'Normalisation' set to 'probability' and 'DisplayStyle' set to stairs'. I need the y-axis as a percentage. I see that I cannot access the histogram.Data values as they are read only and therefore I cannot modify them. I was simply going to multiply them by 100.
Is there anyway to plot this data from the histogram function as: 1) a line plot; 2) with y-axis values as percentage so that when I zoom in and out the percentage also re-scales rather than just setting yticklabels.
Cheers, TJ

回答 (3 件)

Star Strider
Star Strider 2018 年 1 月 17 日

2 投票

I’m not certain what you’ve already done.
The 'YTick' values are available, and you can modify them as you would with any other usual axis object.
Example
d = randn(1, 1000); % Create Data
figure
histogram(d, 50, 'Normalization','probability', 'DisplayStyle','stairs')
figure
histogram(d, 50, 'Normalization','probability', 'DisplayStyle','stairs');
ytix = get(gca, 'YTick')
set(gca, 'YTick',ytix, 'YTickLabel',ytix*100)
Compare the two figures to see the result of recalculating the 'YTick' values in the second histogram.
Andreas Vester
Andreas Vester 2018 年 3 月 8 日

2 投票

Browse the MATLAB documentation for 'yticks' and 'yticklabels'.
One solution might be as follows:
yticklabels(yticks*100)

2 件のコメント

Lisa Kinsale
Lisa Kinsale 2021 年 1 月 28 日
編集済み: Lisa Kinsale 2021 年 1 月 28 日
Thank you, this works,
Additionally do you know how to display the percentage sign (%) on the new yticklabels?
Augusto Gabriel da Costa Pereira
Augusto Gabriel da Costa Pereira 2023 年 7 月 15 日
Here:
ytickformat('percentage');

サインインしてコメントする。

Tony Castillo
Tony Castillo 2020 年 4 月 17 日

0 投票

Hi Sir,
I have plotted a histogram I wonder if is possible to set another axis in the plot like the one in the figure attached, would you mind telling me how to do it, please?.

1 件のコメント

Mahmoud Alkhateb
Mahmoud Alkhateb 2020 年 9 月 17 日
編集済み: Mahmoud Alkhateb 2020 年 9 月 17 日
Hi Tony!
if you want to have the relative frequency on the y-axis (precentage).
You can try this one:
Let's say you have this:
x = let's say you have 30 values bewteen 0 and 25.
L = define the bins you wanna have. e.g. L = 0:5:25; %% this will give you bins that are equal to five starting at 0 and ending at 25.
Now you can plot your histogram as follows
bar (x, histc(x,L)/30, 'histc') %% dividing by 30 which is the number of the total values you have, will give you the relative frequency to evey bin.
Let's say you got 6 values between 0 and 5 (the first bin), what you will get at the y-axis for this bin is 6/30 which is 0.2 (which is 20 %).
I hope this helped!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

質問済み:

TJ
2018 年 1 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by