Help With Graphing on the Same Plot

2 ビュー (過去 30 日間)
Anneliese Fensch
Anneliese Fensch 2020 年 10 月 9 日
コメント済み: Star Strider 2020 年 10 月 10 日
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

採用された回答

Star Strider
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 件のコメント
Anneliese Fensch
Anneliese Fensch 2020 年 10 月 10 日
Thank you so much! The subplot is exactly what I needed.
I'm pretty new to MATLAB, so the little details like this are mysteries to me right now.
Star Strider
Star Strider 2020 年 10 月 10 日
As always, my pleasure!

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

その他の回答 (2 件)

Asad (Mehrzad) Khoddam
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
  1 件のコメント
Anneliese Fensch
Anneliese Fensch 2020 年 10 月 10 日
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.

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


Asad (Mehrzad) Khoddam
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 件のコメント
Anneliese Fensch
Anneliese Fensch 2020 年 10 月 10 日
This didn't quite work either.... I recieved the graph below. Again, I would like it if it were two separate graphs but stacked on top of each other so the x axises are lined up.
Asad (Mehrzad) Khoddam
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 ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by