Combining two stacked plots

54 views (last 30 days)
bef on 4 Aug 2020
Answered: Adam Danz on 19 Nov 2020
I have two 147x1 tables. I plotted them using the stacked plot option and now have two figures. I was wondering how I can go about overlaying the plots given that they have the same x axis.
plot1 = stackedplot(XArray)
plot2 = stackedplot(YArray)
I tried
but it was invalid

Accepted Answer

Star Strider
Star Strider on 4 Aug 2020
I have no idea what ‘XArray’ and ‘YArray’ are, or how they may be related. Apparently, they have different numbers of elements, or plotting them against each other would have worked.
One possibility:
Xt = linspace(min(min(XArray),min(YArray)), max(max(XArray),max(YArray)), numel(XArray));
Yt = linspace(min(min(XArray),min(YArray)), max(max(XArray),max(YArray)), numel(YArray));
plot(Xt, XArray)
hold on
plot(Yt, YArray)
hold off
another possibility:
hold on
hold off
There may still be more possible approaches to this.
Star Strider
Star Strider on 5 Aug 2020
It would have been nice to have known that detail before.
Each table contains a (147x1) vector. Just extract them to vectors using the table2array function:
XArrayVct = table2array(XArray);
YArrayVct = table2array(YArray);
then make appropriate changes in the respective variable names to my code, and imy code should work without further modification. (There are other ways to extract them, however this is easiest.)

Sign in to comment.

More Answers (2)

Seth Furman
Seth Furman on 17 Nov 2020
In cases such as this one, where the tables we want to plot have the same height, we can concatenate them horizontally and call stackedplot.
For example,
>> XArray = array2table(randi(10,147,1),'VariableNames',{'A'});
>> YArray = array2table(randi(10,147,1),'VariableNames',{'B'});
>> stackedplot([XArray,YArray])

Adam Danz
Adam Danz on 19 Nov 2020
Seth Furman's suggestion to concatenate the data prior to creating the stackedplot is a good approach. If that is not an option, for example when the two stackedplots have a different set of x-values, you can create more than 1 stacked plot on a figure by setting the stackedplot parent which can be a:
Figure object | Panel object | Tab object | TiledChartLayout object | GridLayout object
Demo using tiledlayout
fig = figure();
tlo = tiledlayout(fig,1,2);
If the two stackedplot objects have the same x-values, you can link the x-axes so the vertical reference line is the same between both object using the method in this answer.





Community Treasure Hunt

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

Start Hunting!

Translated by