Is it possible to add a secondary y axis while using the stackedplot function?

5 views (last 30 days)
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?

Accepted Answer

Star Strider
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'})
T1 = 250×4 table
x y1 y2 y3 ________ ________ _______ _________ 0 0 0 0 0.040161 0.025231 0.12583 0.22516 0.080321 0.050446 0.24967 0.43875 0.12048 0.075629 0.36953 0.62981 0.16064 0.10076 0.48352 0.78853 0.2008 0.12583 0.58982 0.90675 0.24096 0.15082 0.68675 0.97841 0.28112 0.17572 0.77276 0.99982 0.32129 0.2005 0.84648 0.96989 0.36145 0.22516 0.90675 0.89015 0.40161 0.24967 0.9526 0.76469 0.44177 0.27402 0.98331 0.59997 0.48193 0.2982 0.99839 0.40443 0.52209 0.32219 0.99759 0.18812 0.56225 0.34597 0.98094 -0.037841 0.60241 0.36953 0.94869 -0.26186
figure
stackedplot(T1, {{'y2','y3'}, 'y1'} )
Ax = gca;
get(Ax)
AxesProperties: [2×1 matlab.graphics.chart.stackedplot.StackedAxesProperties] Color: [0.0660 0.4430 0.7450] CombineMatchingNames: 1 DisplayLabels: {2×1 cell} DisplayVariables: {{1×2 cell} 'y1'} EventsVisible: on FontName: 'Helvetica' FontSize: 8 GridVisible: off HandleVisibility: 'on' InnerPosition: [0.1300 0.1100 0.7750 0.8150] Layout: [0×0 matlab.ui.layout.LayoutOptions] LegendLabels: {'T1'} LegendOrientation: 'horizontal' LegendVisible: off LineProperties: [2×1 matlab.graphics.chart.stackedplot.StackedLineProperties] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerEdgeColor: [0.0660 0.4430 0.7450] MarkerFaceColor: 'none' MarkerSize: 6 OuterPosition: [0 0 1 1] Parent: [1×1 Figure] Position: [0.1300 0.1100 0.7750 0.8150] PositionConstraint: 'outerposition' SourceTable: [250×4 table] Title: '' Units: 'normalized' Visible: on XData: [1×0 double] XLabel: 'Row' XLimits: [1 250] XVariable: [] YData: []
Ax1 = Ax.AxesProperties(1)
Ax1 =
StackedAxesProperties with properties: YLimits: [-1.0000 1.0000] YScale: 'linear' LegendLabels: {'y2' 'y3'} LegendLocation: 'northeast' LegendVisible: on CollapseLegend: off
% Ax1.YAxis(2).YAxisLocation = 'right'
You should be able to do this easily using tiledlayout , for example --
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
Natalie
Natalie on 28 Jul 2025
Can you make a shared x axis using the tiledlayout or subplot functions. Will this feature be added to the stackedplot function in the future?
Star Strider
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'})
T1 = 250×4 table
x y1 y2 y3 ________ ________ _______ _________ 0 0 0 0 0.040161 0.025231 0.12583 0.22516 0.080321 0.050446 0.24967 0.43875 0.12048 0.075629 0.36953 0.62981 0.16064 0.10076 0.48352 0.78853 0.2008 0.12583 0.58982 0.90675 0.24096 0.15082 0.68675 0.97841 0.28112 0.17572 0.77276 0.99982 0.32129 0.2005 0.84648 0.96989 0.36145 0.22516 0.90675 0.89015 0.40161 0.24967 0.9526 0.76469 0.44177 0.27402 0.98331 0.59997 0.48193 0.2982 0.99839 0.40443 0.52209 0.32219 0.99759 0.18812 0.56225 0.34597 0.98094 -0.037841 0.60241 0.36953 0.94869 -0.26186
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.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!