Is there a way to insert a common ylabel to secondary axes in a tiled layout?
Mostra commenti meno recenti
Hi everyone,
Dabbling a bit in MATLAB for the past year, mostly for data analysis. I am currently using version 2020 a and recently started enjoying the tiledlayout functionality. I was wondering, if there is a way to insert a shared y-axis title for secondary y-axes? For example, in this code:
t = tiledlayout(2,2);
for i = 1:4
f = figure(1);
ax = nexttile;
yyaxis left
x = (1:10);
y = rand(1,10)*100;
plot(x,y)
yyaxis right
plot(x,sin(y))
end
xlabel(t,'X')
ylabel(t,'Y')
I would like to insert a ylabel for the right axes (lets say 'Y2') that is 'shared' just as 'Y' is for the left axes. I haven't found a solution that worked for me yet, all information I could find would either relate to tiled layouts or two y axes, I haven't found something that combines the two.
I hope you understand my problem, any help is highly appreciated.
Risposta accettata
Più risposte (2)
I had a much more complex figure and with the answer from Rishik I could not get the axes to properly align to the right and to not get overlayed on top of the ytick labels. Here's my minimal implementation which adds a new axes object to the figure:
% reset(groot)
close all;
fig = figure;
t = tiledlayout(2,2);
for i = 1:4
nexttile;
yyaxis left
x = (1:10);
y = rand(1,10)*100;
plot(x,y)
yyaxis right
plot(x,sin(y))
end
h = xlabel(t, 'XAXIS LABEL');
% get font style of left axes
axl = h;
ylabel(t, 'YAXIS LABEL (LEFT)')
axr = axes(t.Parent); % same parent as tiledlayout
% match the new axes position [left bottom width height] to tiledlayout
axr.Position = t.Position;
% compress width of tiledLayout to fit the right axes
t.Position(3) = t.Position(3) - 0.05;
% make new axes invisible
axr.Color = 'none'; % transparent background
axr.XTick = [];
axr.YTick = [];
axr.XColor = 'none';
axr.YColor = 'none';
axr.YAxisLocation = 'right';
ylabel(axr, 'YAXIS LABEL (RIGHT)', 'Color', 'k');
t = tiledlayout(2,2);
for i = 1:4
f = figure(1);
ax = nexttile;
yyaxis left
x = (1:10);
y = rand(1,10)*100;
plot(x,y)
yyaxis right
plot(x,sin(y))
end
addYLabels(t, 'Y1','Y2');
function axOverlay = addYLabels(t, leftLabel, rightLabel)
% Create invisible axes
axOverlay= axes(t.Parent);
t.OuterPosition=axOverlay.Position;
axOverlay.Color = 'none';
pos=["left","right"];
labels={leftLabel, rightLabel};
for i=1:2
% Put ylabel on the right
yyaxis(axOverlay,pos(i))
axOverlay.XColor='none';
axOverlay.YColor='none';
axOverlay.XTick = [];
axOverlay.YTick = [];
ylabel(axOverlay,labels{i},'Color','k');
end
end
Categorie
Scopri di più su Axis Labels in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

