Help With Graphing on the Same Plot

2 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 9 Oct 2020
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 Comments

Sign in to comment.

More Answers (2)

Asad (Mehrzad)  Khoddam
Asad (Mehrzad) Khoddam on 9 Oct 2020
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
  1 Comment
Anneliese Fensch
Anneliese Fensch on 10 Oct 2020
This is not quite what I wanted. When I use this, it graphs them both on the same graph. I would like to have two separate graphs stacked together, so kind of like the example in the picture below, but with my code instead of code on wolves and moose.
I really appreciate the help! I'm quite new to MATLAB, so this might be super easy - I just have no idea what I am doing.

Sign in to comment.


Asad (Mehrzad)  Khoddam
Asad (Mehrzad) Khoddam on 10 Oct 2020
% 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 Comments
Asad (Mehrzad)  Khoddam
Asad (Mehrzad) Khoddam on 10 Oct 2020
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by