我的程序是对太阳系的模拟。我正在使用matplotlib对其进行动画处理,然后在关闭动画人物时,使用模拟数据创建并显示图形。使用Spyder3.3.3,在我第一次运行代码时一切都按预期工作,但是如果我尝试第二次运行该程序,则会在执行动画或代码的图形部分之前弹出一个空白的无响应窗口。我必须让窗口强制关闭该窗口,然后程序崩溃而没有任何错误消息。
我认为问题可能是我最近添加的这段代码:
def onclose(evt):
print("\n" + "Animation closed")
fileout = open("SolarSystem total energy list.txt", "w")
fileout.writelines(self.energy_str)
graph_fig, graph_ax = plt.subplots(figsize=(10, 9))
t_len = len(self.energy)
y = np.around(np.divide(np.array(self.energy), 1e35), 12)
x = np.linspace(0.0, self.timestep_hours*t_len, t_len)
graph_ax.plot(x, y)
graph_ax.set(xlabel="time(hours)", ylabel="total system energy (J [x10^35])")
plt.show(block=False)
self.anim_fig.canvas.mpl_connect("close_event", onclose)
但是,当我删除它时,会出现相同的确切错误。实际上,每隔一秒钟运行一次,该代码似乎就会卡在 this 代码块中(该代码块过去每次都能完美运行):
while True:
self.num_asteroids = int(input("Number of random asteroids (between 0 and 100): "))
if self.num_asteroids >=0 and self.num_asteroids <=100 and isinstance(self.num_asteroids, int) == True :
break
if self.num_asteroids != 0:
for i in range(self.num_asteroids):
while True:
appearancetime = float(input("Time of appearance of random asteroid " + str(i+1) + " in hours: "))
if appearancetime >=0 and appearancetime <self.duration_hours:
self.asteroidlist.append(appearancetime)
break
在spyder ipython控制台中,出现“随机小行星数量(0到100之间):”的输入提示,但我无法键入。因此,它似乎陷在其中,但是我不知道为什么会弹出matplotlib
窗口,因为只有在代码中之后,我才有FuncAnimation()
和plt.show()
行。 / p>
这是我的动画代码:
for i in range(self.num_planets):
self.patches.append(plt.Circle(self.planetlist[i].initial_position*1e-9, radiuslist[i], color=colourlist[i], animated=True))
for i in range(self.asteroidlist.count(0.0)):
self.patches.append(plt.Circle(tuple(self.bodylist[self.num_planets:][i].initial_position*1e-9), random.uniform(0.5, 1), color="#686868", animated=True))
if simulation_type.upper() == "MARSSAT" or simulation_type.upper() == "MARS_SAT" or simulation_type.upper() == "JUPITERSAT" or simulation_type.upper() == "JUPITER_SAT":
self.patches.append(plt.Circle(tuple(self.bodylist[-1].initial_position*1e-9), 0.5, colour="#37FF30", animated=True))
for i in range(self.num_bodies):
self.anim_ax.add_patch(self.patches[i])
self.animation = FuncAnimation(self.anim_fig, self.update, init_func=self.anim_init, frames=self.total_frames, repeat=False, interval=self.interval, blit=True)
如果有帮助,我将spyder设置为使用qt5。