How to change position of both ylabels in yyaxis left, within a subplot enrivonment?
9 ビュー (過去 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!