我想要一个我称之为进度标记的东西,它在音频播放工具中似乎很常见。我认为在matplotlib中,这相当于left / right动画plt.vlines
。我的代码需要2秒钟的数据数组,并创建了音频时间序列可视化。我正在努力创建一条动画的垂直线,该线将在整个图上从0到2线性移动2秒。
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
font = {'weight': 'bold', 'size': 15}
plt.rc('font',**font)
sns.set_style("darkgrid")
testSeries = np.random.randint(-10, 20, 12000)
testSeries = testSeries - testSeries.mean()
fig,axis = plt.subplots(nrows=1,ncols=1,figsize=(18,5),sharex=True)
sns.lineplot(range(0,len(testSeries)),testSeries, color='#007294')
plt.xlim(0, len(testSeries))
axis.set_xlabel("Time (s)", fontsize='large', fontweight='bold')
axis.set_ylabel("Amplitude", fontsize='large', fontweight='bold')
axis.set_xticklabels(['0', '0.3', '0.6', '1', '1.3', '1.6', '2'],fontsize=15)
fig.tight_layout(rect=[0,0,.8,1])
plt.subplots_adjust(bottom=-0.01)
sns.despine()
plt.show()
axvline()
只是返回一个Line2D
对象,因此您可以使用来更新其位置Line2D.set_xdata()
duration = 2 # in sec
refreshPeriod = 100 # in ms
fig,ax = plt.subplots()
vl = ax.axvline(0, ls='-', color='r', lw=1, zorder=10)
ax.set_xlim(0,duration)
def animate(i,vl,period):
t = i*period / 1000
vl.set_xdata([t,t])
return vl,
ani = animation.FuncAnimation(fig, animate, frames=int(duration/(refreshPeriod/1000)), fargs=(vl,refreshPeriod), interval=refreshPeriod)
plt.show()
请注意,刷新率不能保证,它取决于重画图形所花费的时间。您可能需要与一起玩refreshPeriod
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句