I'm making a graph in python with matlibplot that allows users to view temperature over time in heat map. The user should be able to change between temperature per minute and temperature per hour. The animation works great for only minutes and hours, but I want to allow them to switch between the two.
I'm wondering how to alter the frame number an animation is on and change the array it's presenting while it's in the middle of an animation cycle. Or is there a better way to do this? Maybe stop the animation and restart it with new values.
Here's a snippet of the code I'm currently working with. In this code, I'm trying to reset the value of "allZ" which is the temperatures being displayed.
`rows = len(allZ[0][1])
cols = len(allZ[0][1][1])
x, y = np.meshgrid(np.linspace(1, cols, cols), np.linspace(1, rows, rows))
fig, ax = plt.subplots(nrows=1, ncols =1)
ax.set_aspect('equal')
fig.tight_layout(pad=5)
quad = plt.pcolormesh(
x,
y,
np.array(allZ[0][1]),
# shading='gouraud',
cmap='RdBu_r',
vmin = min,
vmax = max)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
fig.colorbar(quad, cax = cax)
# Minute Hour button
rax = fig.add_axes([0.05, 0.05, 0.1, 0.1], facecolor=axcolor)
radio2 = RadioButtons(rax, ('Minute', 'Hour'))
global ani
def init():# not currently called but I did try calling it in the past
quad.set_array(np.array(allZ[0][1]))#index that holds temp
ax.set_title(allZ[0][0])#index that holds time of temp
return quad, ax,
def animate(i):
global allZ
quad.set_array(np.array(allZ[i][1]))#index that holds temp
ax.set_title(allZ[i][0])#index that holds time of temp
return quad, ax,
ani = animation.FuncAnimation(fig,
animate,
frames=len(allZ),
interval=50,
blit=False,
repeat_delay=2500)
# the rest of the button set up
rax = fig.add_axes([0.05, 0.05, 0.1, 0.1], facecolor=axcolor)
radio2 = RadioButtons(rax, ('Minute', 'Hour'))
def timebutton(label):
global dataType
if dataType != label:
dataType = label
ani.pause()
ani.new_frame_seq()
print(f'selected : {label}')
global allZ
if label == "Minute":
# temperature value, min temp, max temp
allZ, min, max = makeZMinute(lotOfData) # what do I do with the new data
if label == "Hour":
# temperature value, min temp, max temp
allZ, min, max = makeZHourly(lotOfData) # what do I do with the new data
radio2.on_clicked(timebutton)
plt.show()`
This currently gives me the following when I press a time button:
UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. anim, that exists until you output the Animation using plt.show() or anim.save(). warnings.warn(