フィルターのクリア

How can I print the mean and standard deviation in the histogram plot?

68 ビュー (過去 30 日間)
Navid
Navid 2023 年 11 月 8 日
コメント済み: Navid 2023 年 11 月 15 日
Hi there
Dear all, I used the syntax below to plot the histogram:
h = histfit(x)
A probability distribution is also obtained from:
pd = fitdist(x,'Normal')
How can I automatically display the mean (mu) and standard deviation (sigma) in the histogram plot?

採用された回答

Image Analyst
Image Analyst 2023 年 11 月 9 日
I don't know if you can do it "automatically" but you can manually use title or text to display text on the chart:
x = randn(1000, 1);
h = histfit(x)
h =
2×1 graphics array: Bar Line
% A probability distribution is also obtained from:
pd = fitdist(x,'Normal')
pd =
NormalDistribution Normal distribution mu = 0.0281218 [-0.0336199, 0.0898636] sigma = 0.994957 [0.953181, 1.04059]
grid on;
% Display the mean and std dev.
caption = sprintf('Mean = %f, StDev = %f', pd.mu, pd.sigma);
title(caption)
  5 件のコメント
Paul Barrette
Paul Barrette 2023 年 11 月 15 日
I do not yet have that tool box (the quote I got for this license is about USD 1000), so am unable to comment on that function.
Navid
Navid 2023 年 11 月 15 日
I am grateful you took the time to share this information with me. Your assistance is highly appreciated.

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2023 年 11 月 9 日
You can text the information into place. Or you can put it into a title or subtitle

Steven Lord
Steven Lord 2023 年 11 月 15 日
The other posters have interpreted "print" in the subject of your Answers post as displaying the numeric value. Another potential interpretation of that (and the word you used later in your question, "display") is to draw lines representing the mean and standard deviations. If you're interested in that visualization of the statistical quantities, use the xline function. I've slightly adapted the "Create Multiple Lines with Labels" example from that page for the example below.
x = randn(1, 1e5); % Sample data
h = histogram(x);
m = mean(x);
s = std(x);
xline([m, m-s, m+s], ":", ["\mu", "\mu- \sigma", "\mu+ \sigma"], ...
"Color", "r", "FontWeight", "bold")
  1 件のコメント
Navid
Navid 2023 年 11 月 15 日
I appreciate your assistance. However, as you know, the xline syntax is only supported by newer versions. Thank you again for your help.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by