Is it possible to add a secondary y axis while using the stackedplot function?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2025-7-28
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 个评论
Star Strider
2025-7-28
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.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


