How to make stack plot with y axis consisting of multiple plots?

I make the plots by the following code using subplots
subplot(2,1,1)
for i=2:size(data,1)
plot_fig(i) = plot(data(1,:),data(i,:),'LineWidth',2.5);
hold on
end
subplot(2,1,2)
plot_fig = plot(data(1,:),y_2(2,:),'LineWidth',2.5);
But I was wondering if can do the same but using stackedplot() considering that my top plot will have multiple plots in it.
The x-axis is same for the above 2 plots. That means these plots can be a stacked plot.

9 Comments

can you attach your data file?
I just attached a similar data. Thanks
writematrix([data;y_2]','stacked.xlsx'); % write to excel file
tbl = readtable('stacked.xlsx'); % convert to table array
stackedplot(tbl); % plot using stackedplot function
Since there are no variables in your attached data files, the o/p plot shows Var1 Var2... VarN
See the fig attached
We can't run your code because variable st and en are missing.
stackedplot(y_2,data')
gives me this plot,
You should be able to run the code now as those variables are not there in the code. Thanks
Hello Vasishta,
The plot that you generated is a normal stack plot. Reffering to your plot, I am looking to have a stacked plot with Var1 to Var5 plotted on the same y-axis(let us say top plot in my figure) but Var 6 is plotted on a different y axis (bottom plot in my figure).
In other words, I want 2 plots stacked but not using subplot, because when I zoom in to one of the plots then other plots are still not zoomed in even though their x axis is the same (in the data file). Please let me know if you still have any questions. Thank you.
"stacked but not using subplot" makes no sense. The ONLY way you can have two plots in one figure is subplots. It sounds like you are looking for the VARS input argument to stackedplot, which will let you group Var1 through Var5 in one subplot and Var6 in another?
Yes, that is right, I am looking for that but with a functionality such that when I zoom in to one of the plots then other plot also shows zoomed in plot (because their plotted x-axis is still the same data)
I think Vasishta Bhargava's answer is what you're looking for.
Also, I just uploaded a function to the file exchange, stackedaxes() which mimicks Matlab's stackedplot but returns the axis handles and allows you to access or add to the content of the figure.

Sign in to comment.

 Accepted Answer

tbl = readtable('stacked.xlsx');
Vars = {{'A','B','C','D','E'},'F'}; % assuming 6 variable names(Var1 ...Var6) as A,B... E, Five of them plotted as one and remaing seperately
stackedplot(tbl,Vars)

11 Comments

+1
@JayVaidya , follow this example. You'll have to convert your data to a table.
Hello Vasishta,
Thanks, this is nearly what I am expecting except that the x axis is needed to be time (which is 1st row of the data, i.e. data(1,:)). In this plot, the variables are plotted against just the row number right? Is there a way I can plot them against the time axis which is the 1st row of the same data.mat?
In other words, I need to plot all the rows from 2:end-1 in one plot and the end row in another, but both these plots are plotted against the row 1 of the data which is actually x axis (data(1,:))
The one that you showed works but has row number as the x axis.
stackedplot(tbl,Vars)
I tried this:
stackedplot(tbl(:,1),tbl(:,2:end),Vars)
This doesn't seem to work. I want to plot the 2:end-1 columns vs 1st column AND end column vs 1st column in the stack plot in the way you showed above. That means the x axis will be 1st column data of the table, not the row number.
stackedplot handles datetime and duration values so there must be a problem with the inputs.
Also, assuming tbl is a table, look at how you're providing the inputs comared to how the documentation explains how to input tables.
So, should I write it this way?
stackedplot(tbl,Vars)
stackedplot(tbl(:,1),'XVariable',xvar)
Take a look at what Vasishta Bhargava did in the answer above and also what the documentation describes (click those link I shared and read).
I did both of them. But, the x axis is still a row number instead of the first column of the data.
If you can put together a minimal working example that allows me to quickly and easily 1) load data, 2) run copy-pasted code that reproduces the problem, I can look into it further.
Vars = {{'B','C','D','E'},'F'};
xvar = "A";
stackedplot(tbl,Vars,'XVariable',xvar)
Hello Vasishta,
Can I not give individual titles to the 2 plot windows in the stackedplot? I am using s.title = 'plot1' in general.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Tags

Asked:

on 18 Nov 2020

Commented:

on 20 Nov 2020

Community Treasure Hunt

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

Start Hunting!