每 2 秒动画一次,Python
Animate lines every 2 seconds, 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()
function。
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)
我用 plt.pause 而不是睡眠,而且有效!
我正在编写一个图表,在数字 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()
function。
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)
我用 plt.pause 而不是睡眠,而且有效!