Combining two stacked plots

조회 수: 35 (최근 30일)
bef
bef 2020년 8월 4일
답변: Adam Danz 2020년 11월 19일
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.
figure(1)
plot1 = stackedplot(XArray)
figure(2)
plot2 = stackedplot(YArray)
I tried
plot(plot1,plot2)
but it was invalid

채택된 답변

Star Strider
Star Strider 2020년 8월 4일
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));
figure
plot(Xt, XArray)
hold on
plot(Yt, YArray)
hold off
grid
another possibility:
figure
plot(XArray)
hold on
plot(YArray)
hold off
grid
There may still be more possible approaches to this.
.
  댓글 수: 2
bef
bef 2020년 8월 5일
편집: bef 2020년 8월 5일
They are tables. However, the only plotting option is stacked plot and such does not "support hold on command." Still not able to plot
Star Strider
Star Strider 2020년 8월 5일
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Seth Furman
Seth Furman 2020년 11월 17일
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 2020년 11월 19일
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);
nexttile(tlo);
stackedplot(rand(40,4),'Parent',tlo)
nexttile(tlo);
stackedplot(rand(55,4),'Parent',tlo)
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.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by