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

采纳的回答

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.

请先登录,再进行评论。

更多回答(2 个)

Asad (Mehrzad) Khoddam
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
% 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
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 CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by