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

5 ビュー (過去 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 件のコメント
Jackson Kock
Jackson Kock 2022 年 3 月 24 日
Hey Peter,
Thanks for the reply!
-Using 'XVariable','X' did the trick.
-I could give the update a try (fingers crossed my other codes don't have any conflicts).
-A follow up question if I may. Are the functionalities of the stacked plot supposed to be limited, compared to the simple plot() function? I am unable to use the 'box' zoom on with the stackedplot. Only can I zoom on the x-axis.
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 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by