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

조회 수: 18(최근 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
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.

채택된 답변

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
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
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개)

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by