Plots combining into one

1 回表示 (過去 30 日間)
Daniel Underwood
Daniel Underwood 2020 年 2 月 27 日
コメント済み: Benjamin Großmann 2020 年 2 月 27 日
Hello all,
I had an issue before in which my plots were combining due to an open hold on function. I am having this issue again but I do not know the solution. I have closed all of my open hold on functions and yet it is still happening. I have included the code below.
%Code 1
w = @(Y,K,k,n) k*Y^n/(K^n + Y^n)
v = @(Y,K,k,n) k*K^n/(K^n + Y^n);
limits = [0 5];
for i=1:5,
vt = @(Y) v(Y,1,10,2);
fplot(vt,limits,'color','r');
end
hold on
for j=1:5,
wt = @(Y) w(Y,1,10,2);
fplot(wt,limits,'color','g');
end
hold off
axis([0 5 0 10])
ylabel('dX/dt')
xlabel('Y')
legend('repressor', 'activator', 'Location', 'northeastoutside')
%Code 2
syms a b X(t) X0;
eqn = diff(X)== a - b*X;
cond = X(0) == X0;
X = dsolve(eqn,cond);
pretty(X)
syms t a b X0
mX = matlabFunction(X,'vars',[t a b X0]);
f1 = @(t) mX(t, 10, 1, 1);
f2 = @(t) mX(t, 50, 5, 1);
f3 = @(t) mX(t, 5, 1, 1);
f4 = @(t) mX(t, 1.2, 0.1, 1);
limits = [0 10];
hold on
fplot(f1, limits, 'k');
fplot(f2, limits, 'r');
fplot(f3, limits, 'b');
fplot(f4, limits, 'g');
hold off
axis([0 10 0 11])
ylabel('X')
xlabel('time')
legend('a=10, b=1', 'a=50, b=5', 'a=5, b=1', 'a=1.2, b=0.1', 'Location', 'SouthEast')

採用された回答

Benjamin Großmann
Benjamin Großmann 2020 年 2 月 27 日
In line 32 of your code there is a (second) "hold on" which acts on the active axes. If you want to have two figures with one axes each, then you have to add a the "figure" command before the hold on command. You can also use one figure with two axes using subplot(2,1,1) before your first for-loop and subplot(2,1,2) before the second "hold on".
  3 件のコメント
Adam
Adam 2020 年 2 月 27 日
You should get into the habit of always keeping the handles to figure, axes, plots etc that you will be applying anything further to.
e.g.
hFig = figure; hAxes = gca;
then you can always use an explicit axes handle for instructions such as:
fplot( hAxes,... )
hold( hAxes, 'on' )
etc.
It requires a little more coding, but makes your life so much easier, knowing exactly where something will be plotted and which axes will have the 'hold' instruction applied, rather than always relying on the current axes being the one you expect it to be.
Benjamin Großmann
Benjamin Großmann 2020 年 2 月 27 日
You are welcome and thank you for accepting the answer. As you are new to this, I would like to add some comments to your code:
  • The comma at the end of your for statement is unneccessary and uncommon
  • Add clearvars, close all and clc at the beginning of your scripts
  • You should see the warning "Function behaves unexpectedly on array inputs" when running your script. Check your functions and add a dot before /*^ if you expect element-wise operations (I think this is the case here)
  • As Adam said, use figure and axes handles, but i do not recommend using gca, but the more explicit way:
hFig = figure('Name','YouNameIt');
hAxes = axes('Parent', hFig);
Now You can also create a figure handle array and set axes inside those figures and dont have to worry about which axes are active.
  • Everything you did to add labels, set axis limits and so on is correct, but check out the methods and properties of the axes handles, e.g. to set the xlabel you could write
hAxes.XLabel.String = 'time';
  • You can also plot explicitly inside one axes by giving the plot function your axes handle as first input argument.
plot(hAxes, x, y);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by