How to set text position in yyaxis?

7 次查看(过去 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