每2秒动画行,Python



我正在编写一个图表,将数字0,10,20(形成数组)等放在一条线上,直到数组的最大值。这是可行的,但是这些行是同时弹出的,但是我想让它们间隔1秒弹出,从0开始,一秒后10,一秒后20,等等。

我做了一些研究,我知道我需要用matplotlib中的FuncAnimation来做。一个函数除以需要出现的东西。但是我不能让它工作。

我真的很喜欢你的帮助!这是我的一些代码

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import gc
from matplotlib.animation import FuncAnimation
#reading my files
#putting data in array called array
#start plotting
array = array - np.nanmin(array)
fig, ax = plt.subplots()
fig.set_size_inches((fileObject.numberOfColumnsInArray + 1) / 2 + 1, (fileObject.numberOfRowsInArray + 1) / 2)

X_Major_ticks = np.arange(1, fileObject.numberOfColumnsInArray + 1)
X_Minor_ticks = np.arange(1.5, fileObject.numberOfColumnsInArray)
Y_Major_ticks = np.arange(1, fileObject.numberOfRowsInArray + 1)
Y_Minor_ticks = np.arange(1.5, fileObject.numberOfRowsInArray)
ax.set_xticks(X_Major_ticks)
ax.set_xticks(X_Minor_ticks, minor=True)
ax.set_yticks(Y_Major_ticks)
ax.set_yticks(Y_Minor_ticks, minor=True)
ax.grid(which='minor', alpha=1, color='black', linewidth=1)
ax.grid(which='major', alpha=0, color='black', linewidth=0.6)

ax.set_xlim(0.5, fileObject.numberOfColumnsInArray + .5)
ax.set_ylim(0.5, fileObject.numberOfRowsInArray + .5)

pos = plt.imshow(array, extent=[.5, fileObject.numberOfColumnsInArray + .5, fileObject.numberOfRowsInArray + .5, .5], cmap=reverse_colourmap(new_cmap), alpha=1)
plt.clim(0,60)
plt.grid(which='both', linestyle='--')    
#        for i in range(fileObject.numberOfColumnsInArray):
#            for j in range(fileObject.numberOfRowsInArray):
#                if ~np.isnan(array[j, i]):
#                    ax.text(i+1, j+1, int(array[j, i]), ha='center', va='center')
for i in range(fileObject.numberOfColumnsInArray-1):
for j in range(fileObject.numberOfRowsInArray):
if abs(array[j, i] - array[j, i+1]) >= blockCutOff:
ax.plot([i+1.5,i+1.5], [j+.5,j+1.5], linewidth=4.5,linestyle=':',color='#FF0000') 

for i in range(fileObject.numberOfColumnsInArray):
for j in range(fileObject.numberOfRowsInArray-1):
if abs(array[j, i] - array[j+1, i]) >= blockCutOff:
ax.plot([i+.5,i+1.5], [j+1.5,j+1.5], linewidth=4.5, linestyle=':',color='#FF0000')

##this part makes the lines on 0 10 20 etc. i want to put tose liens apart every 1 or 2 sec.!!!
# i tried to start a function here but without results.
#def animate(a):
for i in range(fileObject.numberOfColumnsInArray):
for j in range(fileObject.numberOfRowsInArray-1):
while array[j, i] in range(int(np.nanmin(array)),int(np.nanmax(array)), Wavelinetime):
ax.plot([i+.5,i+1.5], [j+1.5,j+1.5], linewidth=3, linestyle='-',color='#000000')
break

#with this i want to animate and call on the function called animate
#ani = FuncAnimation(fig, animate, interval=3000)     
ax.set_title(deflectionTableFile.rsplit ('\')[-1].rsplit ('_')[0] + ': ' + deflectionTableFile.rsplit ('\')[-1].rsplit ('_')[5] + ' wave ' + str(iWaveLabel))                  
plt.gca().invert_yaxis()    
plt.gca().set_aspect('equal', adjustable='box')

cbar_ax = fig.add_axes([.85, 0.15, 0.05, 0.7])
cbar = fig.colorbar(pos, cax=cbar_ax, extend='max', ticks=range(0,61,5))
cbar.minorticks_on()
cbar.ax.set_title('ms')

这个问题最基本的答案是使用Python的时间库的sleep()函数。

for i in range(fileObject.numberOfColumnsInArray):
for j in range(fileObject.numberOfRowsInArray-1):
while array[j, i] in range(int(np.nanmin(array)),int(np.nanmax(array)), Wavelinetime):
ax.plot([i+.5,i+1.5], [j+1.5,j+1.5], linewidth=3, linestyle='-',color='#000000')
# do nothing for x number of seconds e.g. 1
time.sleep(1)

暂停而不是睡觉,这很有效!

最新更新