I am currently developing a behavioral system which will utilize moving bars, rotating bars, alternating fields, and even small random moving objects. Originally all of this was programmed in matlab with great results. However due to incompatibility with the hardware which will eventually use the code, the project has since been transferred over to python.
I have started programming the system using the matplotlib modules in python with some good results. I utilized the animation functions in matplotlib to generate consistent and fluid movement of fast moving objects. However as I dig deeper into programming with matplotlib, I noticed a couple issues. One in particular is that the fluid movement of the objects wasn't exactly as fluid as previously thought.
As I will be utilizing opencv for another part of the behavioral system, I was wondering if opencv has any particular advantages over matplotlib, Particularly in regards to drawing rate and animation.
I will go into more detail below.
Here is part of my script for building the animation, note this this version crashes and I haven't figured out why yet.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import time
fig = plt.figure()
ax = fig.add_subplot(111)
fig.patch.set_facecolor([0,0,0])
fig.tight_layout()
plt.xlim(-100, 100)
plt.ylim(-100, 100)
plt.axis('off')
# List of variables
width = 1
bars = 20
spacing = 20
step = .01
direction = -1
RB = [] # Establish RB as a Python list
for a in range(bars):
RB.append(patches.Rectangle((a*spacing-200,-100), width, 200,
color=[1,0,0], alpha=0.50))
def init():
for a in range(bars):
ax.add_patch(RB[a])
return RB
def animate(i):
for i in range(bars):
temp = np.array(RB[i].get_xy())
if temp[0] > 200:
temp[0] = -199
elif temp[0] < -200:
temp[0] = 199
else:
temp[0] = temp[0] + step*direction;
RB[i].set_xy(temp)
return RB
t = time.time()
plt.show()
while t < timewidow
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=30,
interval=1,
blit=True)
fig.clf()
Code that does work is here. It just that each individual object does not move in sync with each other.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import time
fig = plt.figure()
ax = fig.add_subplot(111)
fig.patch.set_facecolor([0,0,0])
fig.tight_layout()
plt.xlim(-100, 100)
plt.ylim(-100, 100)
plt.axis('off')
# List of variables
width = 1
bars = 20
spacing = 20
step = .01
direction = -1
RB = [] # Establish RB as a Python list
for a in range(bars):
RB.append(patches.Rectangle((a*spacing-200,-100), width, 200,
color=[1,0,0], alpha=0.50))
def init():
for a in range(bars):
ax.add_patch(RB[a])
return RB
def animate(i):
for i in range(bars):
temp = np.array(RB[i].get_xy())
if temp[0] > 200:
temp[0] = -199
elif temp[0] < -200:
temp[0] = 199
else:
temp[0] = temp[0] + step*direction;
RB[i].set_xy(temp)
return RB
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=30,
interval=1,
blit=True)
plt.show()