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

22 ビュー (過去 30 日間)
Jackson Kock
Jackson Kock 2022 年 3 月 24 日
コメント済み: Peter Perkins 2022 年 3 月 25 日
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
% 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:
% 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!


Peter Perkins
Peter Perkins 2022 年 3 月 24 日
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
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 件のコメント
Peter Perkins
Peter Perkins 2022 年 3 月 25 日
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.


その他の回答 (0 件)


Find more on Line Plots 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!

Translated by