python - 每 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')
解决方案
我使用 plt.pause 而不是 sleep 并且它有效!
推荐阅读
- wordpress - 如果运输类别在购物车中,则删除运输方式
- decision-tree - 您可以指定决策树拆分以手动“复制”树吗?
- python - 更改 scipy 图的颜色
- python - 等到网站完全加载后再下载 Scrapy
- javascript - 如何在 React 中将此代码从 Firebase Firestore v8 转换为 Firebase Firestore v9 Modular
- vue.js - 如何删除 url 中有 ?__WB_REVISION 的保存资产的多个网络负载
- c# - 如何绘制一个图,其中每个顶点都是给定大小的矩形,但边可以是任意长度?
- delphi - Delphi服务启动方法
- python-3.x - 用 Python 运行 sed 命令不像在 bash 中那样工作?
- python - 与 UNWIND 合并问题 Neo4j