Is it possible to add a secondary y axis while using the stackedplot function?
5 views (last 30 days)
Show older comments
Is it possible to add a secondary y axis while using the stackedplot function? I have some overlayed data in my stackedplot and I would like to add a secondary y axis for some of the individual plots. Is this possible?
0 Comments
Accepted Answer
Star Strider
on 28 Jul 2025
Apparently not.
Any provision for creating a second y-axis is apparently not available, even when attempting to 'force' it (that can occasionally work with undocumented properties, or at least has in the past). However it is possible to plot two variables on one stackedplot axis.
x = linspace(0, 10, 250).';
ym = sin(x*[1 5 9]*2*pi/10);
T1 = array2table([x ym], VariableNames={'x','y1','y2','y3'})
figure
stackedplot(T1, {{'y2','y3'}, 'y1'} )
Ax = gca;
get(Ax)
Ax1 = Ax.AxesProperties(1)
% Ax1.YAxis(2).YAxisLocation = 'right'
figure
tiledlayout(2,1)
nexttile
yyaxis left
plot(T1.x, T1.y1, DisplayName='y_1')
ylabel('y_1')
yyaxis right
plot(T1.x, T1.y2*2, DisplayName = 'y_2')
grid
ylabel('y_2')
legend(Location='best')
nexttile
plot(T1.x, T1.y3)
grid
xlabel('x')
ylabel('y_3')
.
2 Comments
Star Strider
on 28 Jul 2025
That approach using tiledlayout is the version I illustrated here. It is possible to change its appearance, however tiledlayout is resistant to some manipulations that subplot provides.
This is likely as close as you can get with tiledlayout --
x = linspace(0, 10, 250).';
ym = sin(x*[1 5 9]*2*pi/10);
T1 = array2table([x ym], VariableNames={'x','y1','y2','y3'})
figure
tiledlayout(2,1, TileSpacing='none')
nexttile
yyaxis left
plot(T1.x, T1.y1, DisplayName='y_1')
ylabel('y_1')
Axl = gca;
Axl.XAxis.Color = 'none';
yyaxis right
plot(T1.x, T1.y2*2, DisplayName = 'y_2')
grid
ylabel('y_2')
legend(Location='best')
nexttile
plot(T1.x, T1.y3)
grid
xlabel('x')
ylabel('y_3')
The subplot funciton may offer more options, particularly with respect to the Innerposition and OuterPosition properties (that aren't available in tiledlayout). Much depends on what you want to do.
.
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

