I want my code to run an animation, because i noticed that when i trade the “i” parameter in the np.squeeze(sst[i, :, :]), it makes me able to go to any day in the file. As an example, i can put “0” to go to day 1 of the file. The thing is, i’m not sure how i would implement an update to the image showing up or how to define the time it takes to renew it…
#also, install "basemap-data-hires"
from matplotlib import pyplot as plt
from netCDF4 import Dataset
import numpy as np
from mpl_toolkits.basemap import Basemap
data = Dataset('sst.day.mean.1981.nc')
lats = data.variables['lat'][:]
lons = data.variables['lon'][:]
time = data.variables['time'][:]
sst = data.variables['sst'][:]
mp = Basemap(projection = 'cyl', llcrnrlon = -100, llcrnrlat = -60, urcrnrlon = -20, urcrnrlat=30, resolution ='h' )
lon, lat = np.meshgrid(lons, lats)
x, y = mp(lon, lat)
days = np.arange(0, 10)
for i in days:
c_scheme = mp.pcolor(x, y, np.squeeze(sst[i, :, :]), cmap='jet', latlon = True)
mp.drawcoastlines()
mp.drawstates()
mp.drawcountries()
cbar = mp.colorbar(c_scheme, location = 'right', pad = '10%')
day = i + 1
plt.title(f'Sea surface temperature on the {day}th day of 1981')
plt.clf()
plt.show()
The code i entered here is what i tried to do, because i felt like a for loop would probably get me closer to the end result i was wanting… Obviously, it’s not enough, since i need to code more about the animation.