Adding text to plot with random inputs
3 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Arif Hoq
on 10 Feb 2022
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 Comments
Walter Roberson
on 10 Feb 2022
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 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!