Adding text to plot with random inputs

4 ビュー (過去 30 日間)
Sarah Gomez
Sarah Gomez 2022 年 2 月 10 日
回答済み: Walter Roberson 2022 年 2 月 10 日
clc;
clear;
y=20*randn(1,100000)+80;
histogram(y,11)
title('Normal Distribution')
xlabel('Value');
ylabel('Count');
txt={'\mu = 80'};
text(80,3.3*10^4, txt);
txt={'\sigma = 20'};
text(100, 3.3*10^4, txt);
I have code that takes a random input and creates a histogram that always has the mean = 80 and standard deviation =20. My question is how do I add text to the plot so that my text '\sigma = 20' is always above the exact mean, which should be 80. I've chosen a large y value just to prevent the text being inside the bar but I'd like it to be on top of the bar no matter the value. The standard deviation text can go anywhere, but preferably close to the mean text.

回答 (2 件)

Arif Hoq
Arif Hoq 2022 年 2 月 10 日
y=20*randn(1,100000)+80;
histogram(y,11)
title('Normal Distribution')
xlabel('Value');
ylabel('Count');
txt1={'\mu = 80'};
txt2={'\sigma = 20'};
text(80,3.3*10^4, txt1,'HorizontalAlignment','center','VerticalAlignment','top');
text( 80, 3.3*10^4, txt2,'HorizontalAlignment','center','VerticalAlignment','bottom');

Walter Roberson
Walter Roberson 2022 年 2 月 10 日
y=20*randn(1,100000)+80;
histogram(y,11)
title('Normal Distribution')
xlabel('Value');
ylabel('Count');
actual_mu = mean(y);
actual_sigma = std(y);
txtmu = "\mu = " + round(actual_mu,2);
txtsigma = "\sigma = " + round(actual_sigma,2);
text(actual_mu, 3.3*10^4, txtsigma); %yes, sigma is the one placed at mu
text(actual_mu + 20, 3.3*10^4, txtmu);

カテゴリ

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