I would like to animate the data points which i receive from a DL model. I have followed the answer from here.
I had created my animated plot which satisfies my requirement.
Kindly see the picture below
, where it shows X axes labels Defect & No Defect start to raise from 0 and reach a maximum point
.
I want two conditions to be met, after the animation completed,
- How can I annotate or display a message in the plot after the animation gets completed. (for eg : in my case I want to display maximum value - either defect or No defect in the plot based on the value)
- The picture shows below is for 1 iteration which ran for 100 frames. In the same way, i have another 30 iterations data where each of them should run for 100 frames using
FuncAnimtaion
, which produces animation graph for each iterations and display/annotate the maximum value at the end of iterations. I can give my 30 iterations one by one and produce results, but how one can achieve in code totally.
Code
from matplotlib import animation
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
def barlist(n):
# model detail
b = n + 1
c = X_test[0][:b].reshape((-1, b, X_test.shape[2])) # Input to model
mod = model.predict_on_batch(c) # DL Model which takes input c
pred = np.argmax(mod, axis=2) # Output a array
St = np.count_nonzero(pred == 0)
Rt = np.count_nonzero(pred)
return [St, Rt] # for every frame St, Rt gets updated & animate plot
fig = plt.figure()
axes = plt.axes()
axes.set_ylim([0, 110])
x = np.arange(0,2)
my_xticks = ['Defect','No Defect']
plt.xticks(x, my_xticks)
barcollection = plt.bar(x,[0, 0], width= 0.3)
n = 100 #Number of frames
def animate(i):
y = barlist(i)
for i, b in enumerate(barcollection):
b.set_height(y[i])
anim=animation.FuncAnimation(fig,animate,repeat=False,blit=False,frames=n,interval=50)
plt.show()