Function to update all subplots in each frame def update(t): Update map plots (ax1 and…
- User
Function to update all subplots in each frame
def update(t): # Update map plots (ax1 and ax2) only for the current time step for ax in [ax1, ax2]: ax.clear() ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.BORDERS) ax.add_feature(cfeature.LAND, edgecolor='black') ax.add_feature(cfeature.OCEAN) ax.set_extent([lon_min, lon_max, lat_min, lat_max], crs=ccrs.PlateCarree())
# Update for first map (ax1) data_array1 = pressure[0][t] masked_data1 = np.where(mask, data_array1, np.nan) contours1 = ax1.contour(lon, lat, masked_data1, levels=25, cmap='binary', transform=ccrs.PlateCarree()) label_centers(ax1, contours1, data_array1) # Update for second map (ax2) data_array2 = pressure[1][t] masked_data2 = np.where(mask, data_array2, np.nan) contours2 = ax2.contour(lon, lat, masked_data2, levels=25, cmap='binary', transform=ccrs.PlateCarree()) label_centers(ax2, contours2, data_array2) # Update the line chart scatter points scatter1_10m.set_offsets([datetimes[t], average_windspeeds_aifs_10m[t]]) scatter2_10m.set_offsets([datetimes[t], average_windspeeds_ifs_10m[t]]) scatter1_100m.set_offsets([datetimes[t], average_windspeeds_aifs_100m[t]]) scatter2_100m.set_offsets([datetimes[t], average_windspeeds_ifs_100m[t]]) scatter3_100m_infered_on_ifs.set_offsets([datetimes[t], average_windspeeds_ifs_100m_infered_on_ifs[t]]) # Set titles with the updated time date = starting_date ax1.set_title(f"IFS {date + datetime.timedelta(hours=6 * t)}") ax2.set_title(f"AIFS {date + datetime.timedelta(hours=6 * t)}") return scatter1_10m, scatter2_10m, scatter1_100m, scatter2_100mCreate the animation
ani = FuncAnimation(fig, update, frames=len(datetimes), interval=400, blit=False, repeat=True) #ani.save('animation.html', writer='html') plt.tight_layout() # Adjust layout to prevent overlap plt.close()
Display the animation
HTML(ani.to_jshtml())--------------------------------------------------------------------------- IndexError Traceback (most recent call last) in <cell line: 248>() 246 plt.close() 247 # Display the animation --> 248 HTML(ani.to_jshtml())
4 frames in update(t) 229 # Update the line chart scatter points 230 scatter1_10m.set_offsets([datetimes[t], average_windspeeds_aifs_10m[t]]) --> 231 scatter2_10m.set_offsets([datetimes[t], average_windspeeds_ifs_10m[t]]) 232 scatter1_100m.set_offsets([datetimes[t], average_windspeeds_aifs_100m[t]]) 233 scatter2_100m.set_offsets([datetimes[t], average_windspeeds_ifs_100m[t]])
IndexError: list index out of range how do i account for variable lengths pof the average_windspeeds arrays? preferably id like for the scattered points to just not plot if the datapoint for that time does not exist.
- Assistant