首页 > 解决方案 > Octave 使用“for”语句同时显示两个动画

问题描述

我想使用“for”语句制作 TOF​​-MS 光谱仪(飞行时间质谱仪)的动画。我可以制作一个脚本来动画管内的离子飞行。这是脚本:

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);
  
endfor

%Print slike
print primjer_tof_ms2.png

现在我还想添加一个光谱,该光谱在离子飞行并撞击检测器时绘制。光谱是洛伦兹函数的总和,该函数在特定离子撞击检测器的时刻具有峰值。我制作了一个单独动画这个情节的脚本:

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

x01 = T1;
x02 = T2;
x03 = T3;
fwhm = 1d-7;
t = 0 : 0.4d-7 : 3d-6;
%Spektar = zbroj Lorentz-ovih funkcija (lf)
lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
     ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
     ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);

for k = 1:length(t)
axes ("position", [0.5, 0.67, 0.2, 0.17]);          
plot (t(1:k), lf(1:k), "linewidth", 2);
axis ([0 3d-6 0 5d6])
legend ("off");
set (gca, "xtick", []);
set (gca, "ytick", []);
ht = title(['t = ' num2str(t(k)) ' s']);
pause (0.001);
delete (ht);
endfor
title(['t = ' num2str(t(k))]);

但是当我尝试组合这两个代码时,在第一次迭代后,只有频谱图继续动画直到结束,而以离子作为图形对象的图形停止。我试图将两个图放在同一个“for”循环中:

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  hold on;                  
                    
  x01 = T1;
  x02 = T2;
  x03 = T3;
  fwhm = 1d-7;
  %Spektar = zbroj Lorentz-ovih funkcija (lf)
  lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);
  
  axes ("position", [0.5, 0.67, 0.2, 0.17]);          
  plot (t(1:k), lf(1:k), "linewidth", 2);
  axis ([0 3d-6 0 5d6])
  legend ("off");
  set (gca, "xtick", []);
  set (gca, "ytick", []);
  ht = title(['t = ' num2str(t(k)) ' s']);
  delete (ht);
  title(['t = ' num2str(t(k))]);                  
                  
  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);

hold off; 
endfor

%Print slike
print primjer_tof_ms2.png

有没有人有想法在同一个图形上为这两组对象设置动画?

提前致谢。此致,

伊戈尔

标签: for-loopanimationoctave

解决方案


您正在axes使用此行在循环的每次迭代中创建

axes ("position", [0.5, 0.67, 0.2, 0.17]); 

这也将它们设置为当前的。然后,当你做你的事情时rectangle,这些矩形会绘制在你为光谱创建的小轴上,这些矩形会立即被你创建的新轴覆盖。您隐式创建的第一个轴始终axis equal;保持未使用状态。

相反,您应该在循环外部创建图形对象并在内部使用对它们的引用。大多数绘图函数都可以接受轴的句柄作为它们的第一个参数。或者您可以在调用绘图函数之前显式设置当前轴。最小的示例是:

h = figure;
ax1 = axes('position',[.1,.5,.4,.4]);
ax2 = axes('position',[.5,.1,.4,.4]);

v = -10; 
wmin = 0; dw=0.1; Wmax = 6*pi();
w = [wmin:dw:Wmax];
t0 = 0; dt = 0.01; T = 10;
for t = t0:dt:T;
    x = w/Wmax .*cos(w+t*v);
    y = w/Wmax .*sin(w+t*v);
    plot(ax1,x,y);%specificaly plot to ax1 (also sets it as current)
    axes(ax2)%set ax2 as current axis 
    plot(w,x,w,y);%plot to the current axis
    pause(0.1)
end

然后还有一些警告、意外行为或错误,取决于所选的图形工具包和 octave 版本。我在尝试您的代码时遇到的一个问题是该title对象被 破坏了plot,因此您必须重新创建它而不是更新它的 text 属性。我隐约记得以前在与八度音阶绘图相关的其他情况下不得不求助于其他解决方法。因此,了解它应该如何工作可能会有所帮助,从长远来看,阅读文档可能是值得的。我会推荐别的东西,但这就是我所拥有的。


由于我已经开始通过尝试您的代码来回答,因此您可以使用它的工作版本稍高一些。虽然没有保修。

%Definiranje konstanti
e = 1.609e-19;
u = 1.6605391e-27;
U = 15;
L = 1;
n = 1;

%Ulazni parametri i funkcije
m1 = 4; %He
m2 = 12; %C
m3 = 16; %O
T1 = L * sqrt( (m1 * u)/(2 * n * e * 1000 * U) );
v1 = L/T1;
T2 = L * sqrt( (m2 * u)/(2 * n * e * 1000 * U) );
v2 = L/T2;
T3 = L * sqrt( (m3 * u)/(2 * n * e * 1000 * U) );
v3 = L/T3;

%Opcije grafa
ax1 = axes();%create axes for particles 
axis equal;
xlim ([0 1]);
ylim ([0 0.5]);
axis off;

ax2 = axes ("position", [0.5, 0.67, 0.2, 0.17]);%create axes for spectrum (also activates them)
axis ([0 3d-6 0 5d6])
legend ("off");
set (ax2, "xtick", []);
set (ax2, "ytick", []);

axes(ax1)%activate axes for particles 
%Linije spektrometra
rectangle ("position", [0.0, 0.0, 1, 0.3], 
           "curvature", [0.033, 0.1], 
           "linewidth", 1.5, 
           "facecolor", [0.9, 0.6, 0.4], 
           "facealpha", 0.1);
%Izvor iona           
rectangle ("position", [0.01, 0.13, 0.02, 0.04], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.5, 0.2, 0.8], 
           "facealpha", 0.1); 
%Napon
rectangle ("position", [0.04, 0.0, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
rectangle ("position", [0.04, 0.16, 0.025, 0.14], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");
%Detektor
rectangle ("position", [0.99, 0.1, 0.01, 0.08], 
           "curvature", [0, 0], 
           "linewidth", 1.5, 
           "facecolor", [0.2, 0.2, 0.7], 
           "edgecolor", "none");          

%Tekst
text (-0.05, 0.15, "izvor \niona", "fontsize", 20);
text (0.05, 0.33, "ubrzavajuci \nnapon", "fontsize", 20);
text (0.99, 0.32, "detektor", "fontsize", 20);

%Legenda
%He
rectangle ("position", [0.3, 0.45, 0.01, 0.01], 
           "curvature", [1, 1], 
           "facecolor", [0.2, 0.8, 0.6], 
           "edgecolor", "none");
text (0.35, 0.455, "He^+", "fontsize", 20);
%C         
rectangle ("position", [0.2975, 0.40, 0.015, 0.015], 
           "curvature", [1, 1], 
           "facecolor", [0.4, 0.2, 0.8], 
           "edgecolor", "none");
text (0.35, 0.4075, "C^+", "fontsize", 20);
%O
rectangle ("position", [0.295, 0.35, 0.02, 0.02], 
           "curvature", [1, 1], 
           "facecolor", [0.8, 0.2, 0.4], 
           "edgecolor", "none");
text (0.35, 0.36, "O^+", "fontsize", 20);

%Ioni
t = 0 : 4d-8 : T3;
for k = 1 : length (t)
  axes(ax1)%activate axes for particles
  %Ioni m1,v1
  h11 = rectangle ("position", [0.05 + v1 * t(k), 0.145, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");
  h12 = rectangle ("position", [0.04 + v1 * t(k), 0.148, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none"); 
  h13 = rectangle ("position", [0.03 + v1 * t(k), 0.143, 0.01, 0.01], 
                  "curvature", [1, 1], 
                  "facecolor", [0.2, 0.8, 0.6], 
                  "edgecolor", "none");                  
  %Ioni m2,v2
  h21 = rectangle ("position", [0.05 + v2 * t(k), 0.1425, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none"); 
  h22 = rectangle ("position", [0.04 + v2 * t(k), 0.1455, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");
  h23 = rectangle ("position", [0.025 + v2 * t(k), 0.1405, 0.015, 0.015], 
                  "curvature", [1, 1], 
                  "facecolor", [0.4, 0.2, 0.8], 
                  "edgecolor", "none");               
  %Ioni m3,v3
  h31 = rectangle ("position", [0.05 + v3 * t(k), 0.14, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h32 = rectangle ("position", [0.06 + v3 * t(k), 0.142, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");
  h33 = rectangle ("position", [0.035 + v3 * t(k), 0.138, 0.02, 0.02], 
                  "curvature", [1, 1], 
                  "facecolor", [0.8, 0.2, 0.4], 
                  "edgecolor", "none");                  

  hold on;                  
                    
  x01 = T1;
  x02 = T2;
  x03 = T3;
  fwhm = 1d-7;
  %Spektar = zbroj Lorentz-ovih funkcija (lf)
  lf = ((fwhm^2)./((t-x01).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x02).^2 + fwhm^2))/(pi * fwhm) + ...
       ((fwhm^2)./((t-x03).^2 + fwhm^2))/(pi * fwhm);
  
  plot (ax2,t(1:k), lf(1:k), "linewidth", 2);%plot on spectrum axes (activates them)

        legend ("off");
        set (ax2, "xtick", []);%these properties are reset by each plot, need to set them in loop
        set (ax2, "ytick", []);
        title(['t = ' num2str(t(k))]);                  
                  
  %Pauza izmedu dvije iteracije petlje
  pause (0.001);
  %Nakon svake iteracije, obrisati sliku iona iz prethodne
  delete (h11);
  delete (h12);
  delete (h13);
  delete (h21);
  delete (h22);
  delete (h23);
  delete (h31);
  delete (h32);
  delete (h33);

hold off; 
endfor

推荐阅读