'Animate collision of two balls
I'm trying to make an animation with some input data. The known data includes x,y positions and radius of balls. The program works if animation.FuncAnimation is not called. Once the function is called, error message "ValueError: setting an array element with a sequence." appears. Does the problem come from the way I assign the array? Please tell me how to fix it!
Here is my cod:
import os
import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import matplotlib.animation as animation
# Data collection from text files
Filepath = '/Users/zhengzhiying/Desktop/DEM(CLion)/freefalling_1/'
os.chdir(Filepath)
Flowname = 'Flow'
CLname = 'CL'
Filenumber = len(os.listdir())
# Data_Number = math.ceil(0.5*(Filenumber-1))
Data_Number = 3
ID = [[] for i in range(Data_Number)]
Type = [[] for i in range(Data_Number)]
Phase = [[] for i in range(Data_Number)]
Radius = [[] for i in range(Data_Number)]
PosX = [[] for i in range(Data_Number)]
PosY = [[] for i in range(Data_Number)]
VelX = [[] for i in range(Data_Number)]
VelY = [[] for i in range(Data_Number)]
Vel_Rot = [[] for i in range(Data_Number)]
for file in range(Data_Number):
Flow_path = f"{Filepath}{Flowname}{file}{'.txt'}"
FD = open(Flow_path, 'r')
non_emptyline = FD.read().splitlines()
for index, line in enumerate(non_emptyline):
s = line.split(' ')
ID[file].append([])
Type[file].append([])
Phase[file].append([])
Radius[file].append([])
PosX[file].append([])
PosY[file].append([])
VelX[file].append([])
VelY[file].append([])
Vel_Rot[file].append([])
ID[file][index] = (int)(s[0])
Type[file][index] = (int)(s[1])
Phase[file][index] = (int)(s[2])
Radius[file][index] = (float)(s[3])
PosX[file][index] = (float)(s[4])
PosY[file][index] = (float)(s[5])
VelX[file][index] = (float)(s[6])
VelY[file][index] = (float)(s[7])
Vel_Rot[file][index] = (float)(s[8])
ID = np.asfarray(ID)
Type = np.asfarray(Type)
Phase = np.asfarray(Phase)
Radius = np.asfarray(Radius).T
PosX = np.asfarray(PosX)
PosY = np.asfarray(PosY)
VelX = np.asfarray(VelX)
VelY = np.asfarray(VelY)
Vel_Rot = np.asfarray(Vel_Rot)
# create figure
fig = plt.figure(figsize=(6, 6), dpi=100)
ax = plt.axes(xlim=(+1.0, +3.0), ylim=(-1.0, +2.0))
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_aspect('equal')
ax.tick_params(top=True, right=True, labeltop=True, labelright=True)
# #method1
xy_list = [PosX, PosY]
patch_list = [Circle(xy=(xy[0]), radius=r, color='b', linewidth=1.0, animated=True)
for xy, r in zip(xy_list, Radius)]
h_text = ax.text(0.9, 0.9, 'Time: {}'.format(0), ha='center',
va='center', transform=ax.transAxes)
def init():
for p in patch_list:
ax.add_patch(p)
return patch_list+[h_text]
def pos_update(files):
h_text.set_text('Time: {}'.format(files/1e7))
for p, xy in zip(patch_list, xy_list):
p.set_center(xy[files])
return patch_list+[h_text]
anim = animation.FuncAnimation(fig, pos_update, init_func=init,
frames=Data_Number, blit=True)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
