How to change position of both ylabels in yyaxis left, within a subplot enrivonment?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
dpb
el 31 de Oct. de 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.
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Axis Labels 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!