Stackedplot: Incorrect x-axis values & multi-variable usage

10 views (last 30 days)
Dear All,
I have a sample code to create a stacked plot. I create a table with the x-data, and a few y-datasets. I label the columns of the table respectively.
I make a stackedplot with a couple dataset plotted together. However, the x- axis values correspond to the n'th row of data.
Why aren't the x-values correct?
% Some x-data
x = [2,4,6]';
% Random y-datasets
y = [rand(3,1), rand(3,1), rand(3,1)];
% Names of the table columns
z = {'x','y1','y2','y3'};
% Create the table
t = array2table([x,y],'VariableNames',z);
% Make stackedplot
stackedplot(t,{{'y1','y2'}})
% This yields no error, only the x-data is not what I expect.
I also am trying to figure out how to plot the same varaible multiple times.
If I try the following:
stackedplot(t,{{'y1','y2'},{'y2','y3'}})
% This gives the error:
% Error using stackedplot (line 71)
% Variables to plot must not specify any variable more than once.
I understand what the error is saying. However, what is the solution to this? Must I duplicate the datasets as needed? I am hoping there is a cleaner way.
Thanks a bunch for your help!
Cheers.

Accepted Answer

Peter Perkins
Peter Perkins on 24 Mar 2022
Jackson, you have a table, and while stackedplot knows to plot against time with a timetable, it can't know what to plot against for a table. You might want to be using timetables? If not, I think you need to use
stackedplot(t,{{'y1','y2'}},'XVariable','x')
Also (although I think you know this), {{'y1','y2'}} doesn't actually make a stacked plot, or more accurately, it makes a stack of height one. {'y1','y2'} without the second layer of braces would make two plots.
As for plotting the same var twice, it appears that recent versions allow that. If you can't update, maybe you can make a second copy of the variable in the table.
  2 Comments
Peter Perkins
Peter Perkins on 25 Mar 2022
Yes, stackedplot is more "sealed" than plot, because it has far more moving parts. If you have something that you'd like it to do, you should contact support, we do definitely listen to such requests.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!