Effacer les filtres
Effacer les filtres

Adding text to plot with random inputs

2 vues (au cours des 30 derniers jours)
Sarah Gomez
Sarah Gomez le 10 Fév 2022
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.

Réponses (2)

Arif Hoq
Arif Hoq le 10 Fév 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');

Walter Roberson
Walter Roberson le 10 Fév 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);

Community Treasure Hunt

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

Start Hunting!

Translated by