动画粒子python
我必须写一个脚本来模拟立方体中的气体粒子 问题是图表没有按我所希望的那样更新 这是我的密码动画粒子python,python,animation,tkinter,graph,updates,Python,Animation,Tkinter,Graph,Updates,我必须写一个脚本来模拟立方体中的气体粒子 问题是图表没有按我所希望的那样更新 这是我的密码 import numpy as np import tkinter as tk from tkinter import Canvas,Button,Tk from mpl_toolkits import mplot3d from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanva
import numpy as np
import tkinter as tk
from tkinter import Canvas,Button,Tk
from mpl_toolkits import mplot3d
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
c_cube = input("Donnez le côté de l'enceinte (cm) :")
n_part = input("Donnez le nombre de particules :")
dt = input("Donnez l'intervale de temps δt de déplacement (ms) :")
module_v = input("Donnez la vitesse initiale (module) :")
m_part = input("Donnez la masse des particules (g) :")
pression = 0
volume = int(c_cube)**3
surface = (int(c_cube)**2)
temps = 0.0
x = np.random.uniform(0,int(c_cube),int(n_part))
y = np.random.uniform(0,int(c_cube),int(n_part))
z = np.random.uniform(0,int(c_cube),int(n_part))
teta = np.random.uniform(0,2*np.pi,int(n_part))
sin_teta= np.sin(teta)
cos_teta= np.cos(teta)
phi = np.random.uniform(0,2*np.pi,int(n_part))
sin_phi = np.sin(phi)
cos_phi = np.cos(phi)
vx = int(module_v)*sin_teta*cos_phi
vy = int(module_v)*sin_teta*sin_phi
vz = int(module_v)*cos_teta
dx = vx*float(dt)
dy = vy*float(dt)
dz = vz*float(dt)
def new_coordinates() :
global x
x = np.where(x > float(c_cube),x-dx,x)
x = np.where(x <= float(c_cube),x+dx,x)
global y
y = np.where(y > float(c_cube),y-dy,y)
y = np.where(y <= float(c_cube),y+dy,y)
global z
z = np.where(z > float(c_cube),z-dz,z)
z = np.where(z <= float(c_cube),z+dz,z)
global temps
temps += float(dt)
return x,y,z
可以使用以下输入进行模拟:5、1(对于1个粒子)、0.1、2、1
您将在控制台上看到带有新值的打印(x,y,z,c_立方体)
但是图形没有改变。。。。为什么
(注:为了让它工作,请加入2个代码块)
def update_graph(inc):
new_coordinates()
print(x,y,z,c_cube) # here I print the new coordinates to verify if they are changed
ax1.clear()
ax1.axes.xaxis.set_ticks([])
ax1.axes.yaxis.set_ticks([])
ax1.axes.zaxis.set_ticks([])
ax1.scatter(x, y, z, c='r', depthshade=False, marker='o')
fenetre_simulation = tk.Tk()
fenetre_simulation.title("Simulation Gaz parfait")
fenetre_simulation.configure(background="grey")
fig1 = Figure(figsize=(8, 5), dpi=112)
ax1 = fig1.add_subplot(111, projection='3d')
graph_particules = FigureCanvasTkAgg(fig1, master=fenetre_simulation)
tk.canvas = graph_particules.get_tk_widget()
tk.canvas.grid(row=0, column=0)
tk.Button(fenetre_simulation, text='Quitter', width=10,height=2,
command=fenetre_simulation.destroy).grid(column=2,row=1)
animation_particules = animation.FuncAnimation(fig1, update_graph, interval=800)
#x,y,z = new_coordinates(x,y,z)
fenetre_simulation.mainloop()