How to change position of both ylabels in yyaxis left, within a subplot enrivonment?
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2023-10-31
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!