How to add specific y-value in y-axis of plot
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Zeyad Elreedy
le 17 Déc 2022
Commenté : Zeyad Elreedy
le 18 Déc 2022
Hello, I have the following graph which includes a waveform and its mean. Now, I want to add a value in the y-axis that directly corresponds to the mean, as highlighted below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1234272/image.png)
Is there a way to do this?
0 commentaires
Réponse acceptée
the cyclist
le 17 Déc 2022
Modifié(e) : the cyclist
le 17 Déc 2022
Here is an example using text:
rng default
x = rand(5,1);
meanx = mean(x);
figure
plot(x)
yline(meanx,'r')
text(0.6,meanx,sprintf('%6.2f',meanx))
3 commentaires
the cyclist
le 17 Déc 2022
Yes, you could do something like this:
rng default
x = rand(5,1);
meanx = mean(x);
figure
plot(x)
yline(meanx)
set(gca,'YTick',sort([0.1 : 0.1 : 1, meanx]))
I did the sort(), because the ticks have to be in ascending order, and I think that is probably the easiest way.
You could do something more clever with
set(gca,'YTickLabel',<formatted labels here>)
if the higher precision for the mean is a problem.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!