# How to change position of both ylabels in yyaxis left, within a subplot enrivonment?

16 views (last 30 days)
Sim on 31 Oct 2023
Edited: Sim on 31 Oct 2023
How to change position of both ylabels in yyaxis left, within a subplot enrivonment?
figure();
for i = [1 3 5]
ax1 = subplot(3,2,i);
ax2 = subplot(3,2,i+1);
new_input = rand(10,1);
myfunction_new([ax1,ax2],new_input);
end
function myfunction_new(ax,new_input)
if ~nargin || numel(ax) < 2
figure()
ax = gca();
figure()
ax(2) = gca();
end
% left subplots (left SPs)
hold(ax(1),'on')
yyaxis(ax(1),'left')
plot(ax(1),1:10,new_input,'b-o')
plot(ax(1),1:10,rand(10,1),'r-.o')
ylim(ax(1),[0 1])
ax(1).YAxis(1).Color = 'b';
ax(1).YAxis(2).Color = 'r';
xlabel(ax(1),'X label (left SP)')
ax(1).YAxis(1).Label.String = 'Y1 label (left SP)';
ax(1).YAxis(2).Label.String = 'Y2 label (left SP)';
hold(ax(1),'off')
% QUESTION: I am able to move the left ylabel (in blue) of the left subplots, but how to
% move the right ylabel (in red) still on the left subplots?
h = get(ax(1),'YLabel');
h.Position(1) = h.Position(1) + 0.7;
h.Position(2) = h.Position(2) - 0.1;
% right subplots (right SPs)
hold(ax(2),'on')
bar(ax(2),1:10,new_input)
scatter(ax(2),1:10,rand(10,1),"green",'filled')
xlabel(ax(2),'X label (right SP)')
ylabel(ax(2),'Y label (right SP)')
hold(ax(2),'off')
end

dpb on 31 Oct 2023
hAx=subplot(3,2,1);
yyaxis left
plot(rand(10,1),'bx-')
hLabL=ylabel('Yleft')
yyaxis right
plot(rand(10,1),'ro-')
hLabR=ylabel('Yright');
hLabR.Position=hLabR.Position+[-0.5 0 0];
Don't use gca at all here; create and save handles to the objects desired and use those...
See the yyaxis Tips section on finding handles if do need to search for them, but the far easier tack is to save them when created.
When complete the first subplot and go on to the next, the handle variables become arrays and you can retrieve at will based on the subplot number if don't do everything for each in sequence.
I didn't here for the trivial illustration, but ideally save the line handles as well; I used a "L" and "R" array for left and right, you could make them 2D arrays instead to reduce the number of variables.
Sim on 31 Oct 2023
Edited: Sim on 31 Oct 2023
With small modifications, i.e.
hLabL=ylabel(ax(1),'Yleft');
and
hLabR=ylabel(ax(1),'Yright');
it works! Many thanks!
Here following the entire code with your (slightly modified) suggestions:
figure();
for i = [1 3 5]
ax1 = subplot(3,2,i);
ax2 = subplot(3,2,i+1);
new_input = rand(10,1);
myfunction_new([ax1,ax2],new_input);
end
function myfunction_new(ax,new_input)
if ~nargin || numel(ax) < 2
figure()
ax = gca();
figure()
ax(2) = gca();
end
% -------- dpb solution (part 1) ---------
yyaxis(ax(1),'left')
% ----------------------------------------
% left subplots (left SPs)
hold(ax(1),'on')
plot(ax(1),1:10,new_input,'b-o')
plot(ax(1),1:10,rand(10,1),'r-.o')
ylim(ax(1),[0 1])
ax(1).YAxis(1).Color = 'b';
ax(1).YAxis(2).Color = 'r';
xlabel(ax(1),'X label (left SP)')
ax(1).YAxis(1).Label.String = 'Y1 label (left SP)';
ax(1).YAxis(2).Label.String = 'Y2 label (left SP)';
hold(ax(1),'off')
% -------- dpb solution (part 2) ---------
hLabL=ylabel(ax(1),'Yleft');
hLabL.Position=hLabL.Position+[0.8 0.3 0];
yyaxis(ax(1),'right')
hLabR=ylabel(ax(1),'Yright');
hLabR.Position=hLabR.Position+[-0.9 -0.5 0];
% ----------------------------------------
% right subplots (right SPs)
hold(ax(2),'on')
bar(ax(2),1:10,new_input)
scatter(ax(2),1:10,rand(10,1),"green",'filled')
xlabel(ax(2),'X label (right SP)')
ylabel(ax(2),'Y label (right SP)')
hold(ax(2),'off')
end