How to set text position in yyaxis?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
dpb
el 12 de Oct. de 2022
Editada: dpb
el 12 de Oct. de 2022
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 comentarios
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!