How to reverse one of the y-axis in a stackedplot?

2 views (last 30 days)
I was able to obtain a stackedplot using 2 tables each containing different sets of data. However, I would like reverse tbl1's y axis so there are lower values at the top and higher values at the bottom, rather than vice versa, which is what automatically shows up.
Here is an example of the stacked plot:
stackedplot(tbl1, tbl2, 'XVariable',["x1", "x2"])
How do I reverse the y-axis of tbl1 in this stackedplot?

Accepted Answer

Star Strider
Star Strider on 13 Mar 2023
I doubt that’s possible, since the AxesProperties property doesn’t include YDir.
load outdoors
outdoors(1:3,:)
ans = 3×3 timetable
Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:24 49 51.3 29.61 2015-11-15 01:30:24 48.9 51.5 29.61 2015-11-15 03:00:24 48.9 51.5 29.61
s = stackedplot(outdoors);
% get(s)
s.AxesProperties
ans =
3×1 StackedAxesProperties array with properties: YLimits YScale LegendLabels LegendLocation LegendVisible CollapseLegend
An alternative would be tiledlayout, since it has sepaarate axes thet can be individually controlled —
VN = outdoors.Properties.VariableNames;
figure
tiledlayout(3,1)
for k = 1:3
nexttile
plot(outdoors.Time, outdoors{:,k})
ylabel(VN{k})
end
xlabel('Time')
hf = get(gcf);
Kids1 = get(hf.Children);
Kids2 = Kids1.Children;
Kids2(1).YDir = 'reverse'; % Reverse Y-Axis On Third Plot
That is likely the only option.
.
  2 Comments
Savannah
Savannah on 13 Mar 2023
Thank you very much! It works, but I do not fully understand what you did. Can you explain this part of your code:
hf = get(gcf);
Kids1 = get(hf.Children);
Kids2 = Kids1.Children;
Kids2(1).YDir = 'reverse'; % Reverse Y-Axis On Third Plot
Also, could another option be to use subplot? If so, can you provide an example?
Star Strider
Star Strider on 13 Mar 2023
As always, my pleasure!
Sure!
To completely understand it, you need to remove the end semicolons from ‘Kids1’ and ‘Kids2’ to see what they return. Specifically, ‘Kids2’ returns handles to the three (here) Axes objects created by stackedplot. They all have the expected set of Properties that can then be set, including YDir. (The Axes handles are created in the reverse order in which they appear, so the first one is the last listed Axes handle, and the last listed is the first Axes handle. If the stackedplot array has more than one column, the Axes handles go column-wise and then row-wise, so row 1 column 1, row 1 column2 ... row 2 column 1 row 2 column 2 ... etc. although they appear as a vector. Again, the last listed is the first created.)
.

Sign in to comment.

More Answers (0)

Categories

Find more on Printing and Saving 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!