How to plot more than one graph on a single tile using tiledlayout?

I'm solving 17 species through ode45 (using a for loop with three different initial values). I want to plot all three solutions of a single species on one tile using tiledlayout. With the code I currently have it only plots the third iteration of each species. Could you please provide your expert advise? Thank you!
% Numerically finding the all-positive equilibrium point
% Remove the contents of the workspace
clear
% Set the initial time domain for integration
tstart=0;tend=150;
tspan=[tstart,tend];
y0 = [1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21];
for k = 1:length(y0)
[t,y] = ode45(@Chol_model3,tspan,[y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k)]);
end
tiledlayout(2,2)
nexttile
plot(t,y(:,3),'g','LineWidth', 1,'LineStyle','-');
title('B')
xlabel('Time')
ylabel('Species concentration')
nexttile
plot(t,y(:,4),'k','LineWidth', 1,'LineStyle','-');
nexttile
plot(t,y(:,5),'r','LineWidth', 1,'LineStyle','-');
nexttile
plot(t,y(:,6),'b','LineWidth', 1,'LineStyle','-');

 採用された回答

Star Strider
Star Strider 2022 年 4 月 5 日
I cannot run the posted code, however it is definitely possible to plot more than one series on a specific tile using the hold function —
t = 0:10;
tiledlayout(1,2)
nexttile
plot(t, rand(1,11))
hold on
plot(t, randn(1,11))
hold off
nexttile
plot(t, rand(1,11))
hold on
plot(t, randn(1,11))
hold off
.

13 件のコメント

Ron_S
Ron_S 2022 年 4 月 5 日
My apologies, the ode45 solver uses the Chol_model3.m file with code posted below. (I have not been able to combine these two files into one script file yet).
function dydt = Chol_model3(t,y)
%Read in the parameters from a file
%Chol_parameters
theta=10;
eta=0.01;
mu=5;
gamma=1;
p1=1;
p2=0.8;
k1=0.08;
k2=0.05;
k3=0.1;
k4=0.5;
k5=0.08;
k6=0.01;
k7=0.01;
k8=0.05;
k9=0.1;
k10=10;
k11=0.1;
k12=1;
k13=1;
k14=0.5;
k15=1;
k16=1;
k17=0.01;
k18=1;
R1=0.1;
R2=0.1;
C=y(1);Bc=y(2);B=y(3);Ce=y(4);S=y(5);L=y(6);Ldl=y(7);H=y(8);I=y(9);Cp=y(10);
Cm=y(11);E=y(12);O=y(13);Io=y(14);X=y(15);Xo=y(16);A=y(17);D=y(18);
eqn1=theta*Ce-p1*B*C;
eqn2=p1*B*C-eta*Bc*I;
eqn3=mu-p1*B*C-p2*B;
eqn4=k1*Cp+k3*E-k2*Ce-k4*Ce-theta*Ce+k5*H;
eqn5=p2*B-k9*S;
eqn6=k9*S-k6*L-k15*L*D;
eqn7=k6*L-k7*Ldl;
eqn8=k9*S-k5*H-k16*Io*H;
eqn9=k9*S-k11*I*O-k17*I-eta*Bc*I;
eqn10=k2*Ce+k7*Ldl-k1*Cp-k8*Cp-R1;
eqn11=k8*Cp-k10*Cm - R2;
eqn12=k4*Ce-k3*E-k18*A*E;
eqn13=k10*Cm-k11*I*O-k12*X*O;
eqn14=k11*I*O-k16*Io*H;
eqn15=gamma-k12*X*O;
eqn16=k12*X*O-k13*Xo-k14*Xo;
eqn17=k13*Xo-k18*A*E;
eqn18=k14*Xo-k15*L*D;
dydt=[eqn1; eqn2; eqn3; eqn4; eqn5; eqn6; eqn7; eqn8; eqn9; eqn10; eqn11; eqn12;
eqn13; eqn14; eqn15; eqn16; eqn17; eqn18];
Ron_S
Ron_S 2022 年 4 月 5 日
編集済み: Ron_S 2022 年 4 月 5 日
Without the tiledlayout code, I can get the three graphs to show:
y0 = [1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21];
for k = 1:length(y0)
[t,y] = ode45(@Chol_model3,tspan,[y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k)]);
plot(t,y(:,3),'g','LineWidth', 1,'LineStyle','-');
title('B')
xlabel('Time')
ylabel('Species concentration')
end
If I add the tiledlayout code it only plots one graph. I'm not sure why?
y0 = [1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21];
for k = 1:length(y0)
[t,y] = ode45(@Chol_model3,tspan,[y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k)]);
tiledlayout (2,2)
nexttile
plot(t,y(:,3),'g','LineWidth', 1,'LineStyle','-');
hold on
title('B')
xlabel('Time')
ylabel('Species concentration')
end
hold off
I am still getting used to tiledlayout since I do not use it often.
The first of these draws a plot in each tile, however when I run them together, the second tiledlayout call clears the first set, so I leave the first set to you to experiment with. (This is unlike the subplot function that can create separate subplots in a single figure window. There might be a way to do that with tiledlayout, however I cannot find any documentation for it.) Run them separately to see the results of each.
The problem is that your original code only plots one integration in each tile, similar to my first set here. So the hold call has no effect.
The second creates a single tile and plots all the differential equation solutions in it using hold.
I am not certain what you are doing, however it is definitely possible to plot multiple series in a single tile using hold. It would certainly be possible to do that here, except the code you posted onlly plots one column of the differential equation integration in each iteration of the for loop.
% With 'tiledlayout' & Three Axes —
tspan = [0 150];
% y0 = [1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21];
tiledlayout(3,1)
y0 = 1:10:21;
for k = 1:length(y0)
y0k = ones(1,18)*y0(k);
% [t,y] = ode45(@Chol_model3,tspan,[y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k)]);
[t,y] = ode45(@Chol_model3,tspan,y0k);
nexttile
plot(t,y(:,3),'g','LineWidth', 1,'LineStyle','-');
grid
title('B')
xlabel('Time')
ylabel('Species concentration')
end
% With 'tiledlayout' & One Axes —
tspan = [0 150];
% y0 = [1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21 1:10:21];
tiledlayout(1,1)
nexttile
hold on
y0 = 1:10:21;
for k = 1:length(y0)
y0k = ones(1,18)*y0(k);
% [t,y] = ode45(@Chol_model3,tspan,[y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k) y0(k)]);
[t,y] = ode45(@Chol_model3,tspan,y0k);
plot(t,y(:,3),'g','LineWidth', 1,'LineStyle','-');
grid
title('B')
xlabel('Time')
ylabel('Species concentration')
end
hold off
function dydt = Chol_model3(t,y)
%Read in the parameters from a file
%Chol_parameters
theta=10;
eta=0.01;
mu=5;
gamma=1;
p1=1;
p2=0.8;
k1=0.08;
k2=0.05;
k3=0.1;
k4=0.5;
k5=0.08;
k6=0.01;
k7=0.01;
k8=0.05;
k9=0.1;
k10=10;
k11=0.1;
k12=1;
k13=1;
k14=0.5;
k15=1;
k16=1;
k17=0.01;
k18=1;
R1=0.1;
R2=0.1;
C=y(1);Bc=y(2);B=y(3);Ce=y(4);S=y(5);L=y(6);Ldl=y(7);H=y(8);I=y(9);Cp=y(10);
Cm=y(11);E=y(12);O=y(13);Io=y(14);X=y(15);Xo=y(16);A=y(17);D=y(18);
eqn1=theta*Ce-p1*B*C;
eqn2=p1*B*C-eta*Bc*I;
eqn3=mu-p1*B*C-p2*B;
eqn4=k1*Cp+k3*E-k2*Ce-k4*Ce-theta*Ce+k5*H;
eqn5=p2*B-k9*S;
eqn6=k9*S-k6*L-k15*L*D;
eqn7=k6*L-k7*Ldl;
eqn8=k9*S-k5*H-k16*Io*H;
eqn9=k9*S-k11*I*O-k17*I-eta*Bc*I;
eqn10=k2*Ce+k7*Ldl-k1*Cp-k8*Cp-R1;
eqn11=k8*Cp-k10*Cm - R2;
eqn12=k4*Ce-k3*E-k18*A*E;
eqn13=k10*Cm-k11*I*O-k12*X*O;
eqn14=k11*I*O-k16*Io*H;
eqn15=gamma-k12*X*O;
eqn16=k12*X*O-k13*Xo-k14*Xo;
eqn17=k13*Xo-k18*A*E;
eqn18=k14*Xo-k15*L*D;
dydt=[eqn1; eqn2; eqn3; eqn4; eqn5; eqn6; eqn7; eqn8; eqn9; eqn10; eqn11; eqn12;
eqn13; eqn14; eqn15; eqn16; eqn17; eqn18];
end
.
Ron_S
Ron_S 2022 年 4 月 5 日
編集済み: Ron_S 2022 年 4 月 5 日
Thanks so much, I'll experiment with the examples you provided!
I had a go at using subplot too but could not get it to work. Would that perhaps be another option to explore further?
My main opjective is to plot each species with its associated graphs in a seperate tile.
Star Strider
Star Strider 2022 年 4 月 5 日
編集済み: Star Strider 2022 年 4 月 5 日
My pleasure!
I had a go at using subplot too but could not get it to work. Would that perhaps be another option to explore further?
Yes. I have no specific preference for subplot, tiledlayout, or stackedplot. They each have properties that work best in specific applications.
My main opjective is to plot each species with its associated graphs in a seperate tile.
Any of the three options that I mentioned would work for that. You need to decide on which is best. Regardless, I will do what I can to help you to get the plots working as you want them to work.
.
Ron_S
Ron_S 2022 年 4 月 5 日
The tiledlayout with one axis works perfect, thank you!
Star Strider
Star Strider 2022 年 4 月 5 日
As always, my pleasure!
I did not ask this before because it was not relevant, however just out of curiosity, what are you modeling?
Ron_S
Ron_S 2022 年 4 月 5 日
編集済み: Ron_S 2022 年 4 月 5 日
Cellular cholesterol homeostasis - part of my PhD.
Star Strider
Star Strider 2022 年 4 月 5 日
Fascinating!
Is this something I can look forward to reading about in Science or the NEJM?
Ron_S
Ron_S 2022 年 4 月 6 日
Yep, hoping to send to the Journal of The Royal Society Interface very soon!
Star Strider
Star Strider 2022 年 4 月 6 日
Getting it published in the JRS is impressive! I’d appreciate a link to it. It would be nice to at least be able to read the abstract. Unless it’s open-access, I likely won’t be able to read the entire article.
Ron_S
Ron_S 2022 年 4 月 7 日
Hopefully it will be accepted! I'll keep you posted.
Star Strider
Star Strider 2022 年 4 月 7 日
Thank you!
If you also send it to me as an email through my profile page, please provide a context so I’ll know what it refers to, in addition to including a link to this thread. If it’s just a link, I’ll probably not recognise it, and I’m averse to clicking on uinknown links.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by