Is there a way to plot multiple graphs in one "subplot" of a stackedplot figure?

31 views (last 30 days)
Say I plot data with the stackedplot function. Now I want to add fits (or for simplicity: lines) to the graphs.
starttime=datetime(2018,1,1);
endtime=datetime(2018,10,1);
A=(starttime:calmonths:endtime)';
B=rand(10,3);
tableAB=table(A,B(:,1),B(:,2),B(:,3));
stackedplot(tableAB,'XVariable','A');
%add fits/lines to each graph
line([starttime endtime],[0.4 0.4],'Color','red','LineWidth',2); %for the first graph
line([starttime endtime],[0.5 0.5],'Color','green','LineWidth',2); %for the second graph
line([starttime endtime],[0.6 0.6],'Color','blue','LineWidth',2); %for the third graph
This throws the error
Error using * (line 9)
Only Cartesian axes support non-numeric data
Of course I would not expect the lines to be attached to the right plot (since there is no parameter for that [?] / I did not set the parameter).
How do I get my lines/fits (attached to the right graph) into my stackedplot figure?

Accepted Answer

Duncan Po
Duncan Po on 7 Mar 2019
stackedplot can plot multiple lines in the same axes. One way is to have a non-column matrix as one of your table variables:
x = randn(10,2); y = randn(10,1);
t = table(x,y);
stackedplot(t)
The first axes now contains 2 lines. Another way is to set the DisplayVariables property to a nested cellstr (or cell array of cell array of chars). Then you can plot multiple data variables in the same axes.
x = randn(10,1); y = randn(10,1);
t = table(x,y);
stackedplot(t, {{'x', 'y'}})
  1 Comment
JD4
JD4 on 26 Mar 2019
Thank you so much, that is exactly what I was looking for. For anyone who is also looking into this, I have one more cool trick:
When adjusting LineProperties like PlotType, Marker, MarkerSize, etc. like this
x = randn(10,2); y = randn(10,1);
t = table(x,y);
s=stackedplot(t)
s.LineProperties(1).PlotType='scatter';
s.LineProperties(1).Marker='o';
s.LineProperties(1).MarkerSize=6;
s.LineProperties(1).MarkerFaceColor=[0 1 1];
s.LineProperties(1).MarkerEdgeColor=[0 0 0];
you change the properties of all lines (or we should rather call them graphs) in your first plot. If you want to adjust LineProperties of the lines individually, you can just add additional entries, i.e. additional cells for PlotType and Marker and additional matrix entries for MarkerSize etc. Here an example for two graphs, where the first is a scatter plot and the second is a line.
s=stackedplot(t)
s.LineProperties(1).PlotType={'scatter','plot'};
s.LineProperties(1).Marker={'o','none'};
s.LineProperties(1).MarkerSize=6;% could replace 6 with [6 3] for example
s.LineProperties(1).MarkerFaceColor=[0 1 1];
s.LineProperties(1).MarkerEdgeColor=[0 0 0];
To change LineProperties in the second plot, change the 1 behind LineProperties to a 2.
Here is a minimal example that looks somewhat like what I originally planned:
x(:,1) = randn(1,10);
x(:,2) = repmat(mean(x(:,1)),10,1); % takes the mean of the first column and
% fills the column with it
y = rand(10,1);
t = table(x,y);
S=stackedplot(t);
S.LineProperties(1).Marker={'o','none'};
S.LineProperties(1).PlotType={'scatter','plot'};
S.LineProperties(1).MarkerSize=6;
S.LineProperties(1).MarkerFaceColor=[0 1 1];
S.LineProperties(1).MarkerEdgeColor=[0 0 0];
ministackedplots.png
Hope this helps anyone having the same problem!

Sign in to comment.

More Answers (1)

Sihui Liu
Sihui Liu on 7 Mar 2019
Hi,
I understand that you are having trouble with adding 'fits' or 'lines' to stackedplots.
However, stackedplot is a “closed” plot, meaning it doesn't allow adding other plots freely to its axes.
You may want to try other plot functions if you want to add fits to your figures.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!