How to set text position in yyaxis?

조회 수: 9 (최근 30일)
galaxy
galaxy 2022년 10월 12일
편집: dpb 2022년 10월 13일
Hi all
I tried to set text position as following. It was OK.
t = 1:0.1:10;
yyaxis('left');
plot(t, sin(t), 'b.-', 'LineWidth', 2);
ylabel('sin');
hold on
yyaxis('right');
plot(t, cos(t)*2, 'g.-', 'LineWidth', 2);
ylabel('cos');
xlabel('Time[ms]');
grid on;
yyaxis('left');
text(4, 0.5,'left=0.5') %I would like this to be referenced to the 2nd y-axis
yyaxis('right');
text(4, 0.5,'right=0.5') %I would like this to be referenced to the 1st y-axis
hold off
But now I want to change text by set
t = 1:0.1:10;
yyaxis('left');
plot(t, sin(t), 'b.-', 'LineWidth', 2);
ylabel('sin');
hold on
yyaxis('right');
plot(t, cos(t)*2, 'g.-', 'LineWidth', 2);
ylabel('cos');
xlabel('Time[ms]');
grid on;
hText = nan(1, 2);
for cnt = 1:2
hText(cnt) = text(NaN, NaN, '', ...
'BackgroundColor', 'yellow');
end
yyaxis('left');
set(hText, 'Position', [4, 0.5], 'String', 'left=0.5');
%text(4, 0.5,'left=0.5') %I would like this to be referenced to the 2nd y-axis
yyaxis('right');
set(hText, 'Position', [4, 0.5], 'String', 'right=0.5');
%text(4, 0.5,'right=0.5') %I would like this to be referenced to the 1st y-axis
hold off
you can see that, can't display [left=0.5] following 1st y-axis.
Do anyone know why? Please explain and tell me how to fix.
thank you

답변 (1개)

dpb
dpb 2022년 10월 12일
편집: dpb 2022년 10월 12일
t = 1:0.1:10;
yyaxis('left');
plot(t, sin(t), 'b.-', 'LineWidth', 2);
ylabel('sin');
text(4,0.5,'left=0.5','BackgroundColor','y');
yyaxis('right');
plot(t, cos(t)*2, 'g.-', 'LineWidth', 2);
ylabel('cos');
xlabel('Time[ms]');
text(4,0.5,'right=0.5','BackgroundColor','y');
yl=ylim;yticks(linspace(yl(1),yl(2),11)) % make grids align both axes
grid on;
Do what needs doing on each axis when there to begin with.
While what you had will work; it's a lot more trouble switching back and forth than finishing the job when there.
The specific problem in your code is the two lines
...
set(hText, 'Position', [4, 0.5], 'String', 'left=0.5');
...
set(hText, 'Position', [4, 0.5], 'String', 'right=0.5');
...
hText is the array of BOTH handles; each call sets the position of both in turn to the same place on the axis presently in focus. So, the last axis to have focus and the call wins.
BTW when allocating space for future graphics objects, instead of
hText = nan(1, 2);
use
hText = gobjects(1, 2);
which is expressly for the purpose.
  댓글 수: 4
galaxy
galaxy 2022년 10월 13일
I need to use function and call other function.
So, I can not use yyaxis('left'); yyaxis('right'); anytime.
But, I think that can use set() at same time by initialize first.
anw, thank you your anwser. This topic will close in here.
dpb
dpb 2022년 10월 13일
편집: dpb 2022년 10월 13일
What you think and what happens have been demonstrated to not be consonant in results desired...

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by