Help With Graphing on the Same Plot
2 次查看(过去 30 天)
显示 更早的评论
Hello! I am hoping someone can help me find a way to have these graphs lined up so the x-axis with months on them line up. ( 200 months in the scallops line up with 200 months in the ray population and such). I think the name for it is a stacked time series.
Here's my code! Thank you very much in advance.
% Trohpic Cascade to calculate the changes in population in scallops and
% cownose rays based on interactions with each other
clear
% Inputs
R(1) = 100; % Initial poplation of cownose rays
B(1) = 10000; % Initial population of scallops
dR = .02; % Death rate of rays in abscence of scallops
gamma = 0.05; % Growth rate of scallops in absence of cownose rays
T = 1000; % Time in months that the simulation will be run for
Rcrit = 83; % Critical value of rays to support a stable scallop population
Bcrit = 6000; % Critical value of scallops needed to support a stable cownose ray population
for t = 1 : T-1
B(t+1) = B(t) + computeDeltaB(gamma, R(t), Rcrit, B(t));
R(t+1) = R(t) + computeDeltaR(dR, B(t), Bcrit, R(t));
end
figure;
plot(B);
hold on
xlabel('time (months)', 'FontSize' ,18);
ylabel ('B (Scallops)', 'FontSize', 18);
title('Scallops Over Time', 'FontSize', 18)
figure;
plot(R);
xlabel('time (months)', 'FontSize', 18);
ylabel ('R (Rays)', 'FontSize', 18);
title(' Rays over Time', 'FontSize', 18)
hold off
0 个评论
采纳的回答
Star Strider
2020-10-9
Plotting specific time vectors as well as ‘R’ or ‘B’ might do what you want. (We cannot run your code.)
Example —
plot(tR, R)
and
plot(tB, B)
Second option — plot them both on the same axes:
figure
plot(tR, R)
hold on
plot(tB,B)
hold off
xlabel('time (months)', 'FontSize' ,18);
ylabel('Population')
legend('Rays', 'Scallops', 'Location','best')
.
4 个评论
更多回答(2 个)
Asad (Mehrzad) Khoddam
2020-10-9
The second figure command, creates a new figure,
you can comment it out:
% The second graph
%figure;
plot(R);
xlabel('time (months)', 'FontSize', 18);
ylabel ('R (Rays)', 'FontSize', 18);
title(' Rays over Time', 'FontSize', 18)
hold off
Asad (Mehrzad) Khoddam
2020-10-10
% Trohpic Cascade to calculate the changes in population in scallops and
% cownose rays based on interactions with each other
clear
% Inputs
R(1) = 100; % Initial poplation of cownose rays
B(1) = 10000; % Initial population of scallops
dR = .02; % Death rate of rays in abscence of scallops
gamma = 0.05; % Growth rate of scallops in absence of cownose rays
T = 1000; % Time in months that the simulation will be run for
Rcrit = 83; % Critical value of rays to support a stable scallop population
Bcrit = 6000; % Critical value of scallops needed to support a stable cownose ray population
for t = 1 : T-1
B(t+1) = B(t) + computeDeltaB(gamma, R(t), Rcrit, B(t));
R(t+1) = R(t) + computeDeltaR(dR, B(t), Bcrit, R(t));
end
yyaxis left;
plot(t,B);
ylabel ('B (Scallops)', 'FontSize', 18);
title('Scallops Over Time', 'FontSize', 18);
yyaxis right
plot(t,R);
xlabel('time (months)', 'FontSize', 18);
ylabel ('R (Rays)', 'FontSize', 18);
title(' Rays over Time', 'FontSize', 18)
xlabel('time (months)', 'FontSize' ,18);
hold off
2 个评论
Asad (Mehrzad) Khoddam
2020-10-10
If you want to have two different plots, use subplot:
% Trohpic Cascade to calculate the changes in population in scallops and
% cownose rays based on interactions with each other
clear
% Inputs
R(1) = 100; % Initial poplation of cownose rays
B(1) = 10000; % Initial population of scallops
dR = .02; % Death rate of rays in abscence of scallops
gamma = 0.05; % Growth rate of scallops in absence of cownose rays
T = 1000; % Time in months that the simulation will be run for
Rcrit = 83; % Critical value of rays to support a stable scallop population
Bcrit = 6000; % Critical value of scallops needed to support a stable cownose ray population
for t = 1 : T-1
B(t+1) = B(t) + computeDeltaB(gamma, R(t), Rcrit, B(t));
R(t+1) = R(t) + computeDeltaR(dR, B(t), Bcrit, R(t));
end
subplot(2,1,1)
plot(t,B);
xlabel('time (months)', 'FontSize', 18);
ylabel ('B (Scallops)', 'FontSize', 18);
title('Scallops Over Time', 'FontSize', 18);
subplot(2,1,2)
plot(t,R);
xlabel('time (months)', 'FontSize', 18);
ylabel ('R (Rays)', 'FontSize', 18);
title(' Rays over Time', 'FontSize', 18)
xlabel('time (months)', 'FontSize' ,18);
hold off
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!