How to set text position in yyaxis?
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!