I can't figure out why the plot don't refresh when the slider is updated. I'm using Jupiter notebook and I choose the backend with 'nbAgg' parameter.
Initialization code :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import use as m_use
from matplotlib.widgets import Slider
m_use('nbAgg')
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2,figsize=(7, 5))
plt.subplots_adjust(left=0.1,right=.8)
There is a animation which lunch this function :
def updateData(curr,divider=7):
global ax1, ax2, ax3, ax4
for ax in (ax1, ax2, ax3, ax4):
ax.cla()
if curr <= 679 :
my_b = int(np.around(curr/divider)+3)
else : my_b = 100
multi = 100
ax1.hist(x1[:curr*multi],bins=my_b)
ax2.hist(x2[:curr*multi],bins=my_b)
ax3.hist(x3[:curr*multi],bins=my_b)
ax4.hist(x4[:curr*multi],bins=my_b)
fig.suptitle('Frame {} on 100'.format((curr+1)))
return None
The animation :
simulation = animation.FuncAnimation(fig=fig, func=updateData, frames=10,
blit=False, interval=1, repeat=False)
Here the slider which stuck me :
slider_ax = plt.axes([.9, 0.45, 0.01, 0.3])
slider = Slider(ax=slider_ax,label='Divider \nfor bins',valmin=1,valmax=15, valinit=7, orientation='vertical',valfmt='%.0f',track_color='black',facecolor='red',
handle_style={'facecolor':'k','edgecolor':'#86a2cf','size':20})
def update():
anim_2 = animation.FuncAnimation(fig=fig, func=updateData, frames=20,
blit=False, interval=1, repeat=False)
This below function don't work as expected :
slider.on_changed(update)
simulation = animation.FuncAnimation(fig=fig, func=updateData, frames=10,
blit=False, interval=1, repeat=False)
plt.show()