How can one put mean and std of the data into histogram using annotation?

201 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 8 月 13 日
編集済み: alpedhuez 2020 年 8 月 15 日
  5 件のコメント
Steven Lord
Steven Lord 2020 年 8 月 15 日
Can you show a picture of what you want the histogram with annotation to look like? That may help us suggest the best options.
Personally I might just go with xline objects.
x = randn(1, 1e5);
histogram(x)
xline(mean(x), 'LineStyle', '--', 'Color', 'r', 'LineWidth', 2)
xline(std(x), 'LineStyle', ':', 'Color', 'c', 'LineWidth', 1.5)
xline(-std(x), 'LineStyle', ':', 'Color', 'c', 'LineWidth', 1.5)
alpedhuez
alpedhuez 2020 年 8 月 15 日
編集済み: alpedhuez 2020 年 8 月 15 日
  • Is it possible to have a box in text()?
  • xline example does not display mean and std numbers. how can one display these info?

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

採用された回答

Image Analyst
Image Analyst 2020 年 8 月 15 日
Perhaps you wanted this:
data = rand(1, 100000);
numBins = 400;
histogram(data, numBins, 'EdgeColor', 'none');
grid on;
xlabel('Data Value', 'FontSize', 15);
ylabel('Count', 'FontSize', 15);
% Compute mean and standard deviation.
mu = mean(data)
sigma = std(data)
% Indicate those on the plot.
xline(mu, 'Color', 'g', 'LineWidth', 2);
xline(mu - sigma, 'Color', 'r', 'LineWidth', 2, 'LineStyle', '--');
xline(mu + sigma, 'Color', 'r', 'LineWidth', 2, 'LineStyle', '--');
ylim([0, 400]); % Give some headroom above the bars.
yl = ylim;
sMean = sprintf(' Mean = %.3f\n SD = %.3f', mu, sigma);
% Position the text 90% of the way from bottom to top.
text(mu, 0.9*yl(2), sMean, 'Color', 'r', ...
'FontWeight', 'bold', 'FontSize', 12, ...
'EdgeColor', 'b');
sMean2= sprintf('Histogram with %d bins. Mean = %.3f. SD = %.3f', numBins, mu, sigma);
title(sMean2, 'FontSize', 15);
Adapt as needed.
  1 件のコメント
alpedhuez
alpedhuez 2020 年 8 月 15 日
編集済み: alpedhuez 2020 年 8 月 15 日
Thank you. True text() is better.

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

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 8 月 15 日
Hi,
A user can fully control the location of the legend by indicating its location as:
legend({sprintf('mean = %3.2f, std = %3.2f', mean(D), std(D))}, 'location', 'best') % Puts the legend automatically on an empty spot
legend({sprintf('mean = %3.2f, std = %3.2f', mean(D), std(D))}, 'location', 'southwest') % Puts on the lower left corner
legend({sprintf('mean = %3.2f, std = %3.2f', mean(D), std(D))}, 'location', 'southeast') % Puts on the lower right corner
legend({sprintf('mean = %3.2f, std = %3.2f', mean(D), std(D))}, 'location', 'northwest') % Puts on the upper left
legend({sprintf('mean = %3.2f, std = %3.2f', mean(D), std(D))}, 'location', 'southeast') % Puts on the upper right
  1 件のコメント
alpedhuez
alpedhuez 2020 年 8 月 15 日
編集済み: alpedhuez 2020 年 8 月 15 日
I wanted more detailed location specificatin like one can do with annotation.

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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by