How to add specific y-value in y-axis of plot

조회 수: 12 (최근 30일)
Zeyad Elreedy
Zeyad Elreedy 2022년 12월 17일
댓글: Zeyad Elreedy 2022년 12월 18일
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.
Is there a way to do this?

채택된 답변

the cyclist
the cyclist 2022년 12월 17일
편집: the cyclist 2022년 12월 17일
You could use either the text or annotation function to do that.
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
the cyclist
the cyclist 2022년 12월 17일
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.
Zeyad Elreedy
Zeyad Elreedy 2022년 12월 18일
Great, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by