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

5 vues (au cours des 30 derniers jours)
Jackson Kock
Jackson Kock le 24 Mar 2022
Commenté : Peter Perkins le 25 Mar 2022
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.

Réponse acceptée

Peter Perkins
Peter Perkins le 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 commentaires
Jackson Kock
Jackson Kock le 24 Mar 2022
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 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by