Adding text to plot with random inputs
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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');
0 个评论
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!