-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_mean_anim.py
79 lines (59 loc) · 3.32 KB
/
plot_mean_anim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import matplotlib.pyplot as plt
from cartopy import crs as ccrs
from matplotlib.animation import FuncAnimation
import xarray as xr
import numpy as np
from read_sla_currents import lat, lon
from temporal_mean import sla_annual_mean, ug_annual_mean, vg_annual_mean, sla, ug, vg
############# plotting animation of annual mean of SLA, geostrophic currents
fig = plt.figure(figsize=(10, 8))
ax = plt.subplot(1, 1, 1, projection=ccrs.LambertAzimuthalEqualArea(central_longitude=0.0, central_latitude=80.0)) # LambertAzimuthalEqualArea / NorthPolarStereo
# ax = plt.subplot(1, 1, 1, projection=ccrs.NorthPolarStereo()) # LambertAzimuthalEqualArea / NorthPolarStereo
ax.coastlines(resolution='50m', linewidth=1)
# plot SLA with colorbar
im = ax.pcolormesh(lon, lat, sla_annual_mean[2011][:, :], transform=ccrs.PlateCarree(), cmap=plt.cm.seismic)
# im = ax.pcolormesh(lon, lat, sla[0, :, :], transform=ccrs.PlateCarree(), cmap=plt.cm.seismic)
cbar = plt.colorbar(im, ax=ax, orientation='vertical')
cbar.set_label('Sea Level Anomaly (m)')
im.set_clim(-1, 1)
# plot ug vg first time step using xarray quiver and subsampling resolution to reduce density
ds_velocity = xr.Dataset({'ug': (['lat', 'lon'], ug_annual_mean[2011]),
'vg': (['lat', 'lon'], vg_annual_mean[2011])},
coords={'lon': lon[0,:], 'lat': lat[:,0]})
# ds_velocity = xr.Dataset({'ug': (['lat', 'lon'], ug[0,:,:]),
# 'vg': (['lat', 'lon'], vg[0,:,:])},
# coords={'lon': lon[0,:], 'lat': lat[:,0]})
ds_velocity.sel(lon=slice(None, None, 12), lat=slice(None, None, 4)).\
plot.quiver('lon', 'lat', 'ug', 'vg', ax=ax, transform=ccrs.PlateCarree(), scale=1.5)
# animation function that updates each time step
def update(frame):
ax.clear()
# update SLA
im = ax.pcolormesh(lon, lat, sla_annual_mean[frame][:, :], transform=ccrs.PlateCarree(), cmap=plt.cm.seismic)
# im = ax.pcolormesh(lon, lat, sla[frame, :, :], transform=ccrs.PlateCarree(), cmap=plt.cm.seismic)
ax.coastlines(resolution='50m', linewidth=1)
# im.set_array(sla_annual_mean[frame].ravel()) # Update sla. after ax.clear removed it
# update ug vg
ds_velocity = xr.Dataset({'ug': (['lat', 'lon'], ug_annual_mean[frame].data),
'vg': (['lat', 'lon'], vg_annual_mean[frame].data)},
coords={'lon': lon[0,:], 'lat': lat[:,0]})
# ds_velocity = xr.Dataset({'ug': (['lat', 'lon'], ug[frame,:,:]),
# 'vg': (['lat', 'lon'], vg[frame,:,:])},
# coords={'lon': lon[0,:], 'lat': lat[:,0]})
ds_velocity.sel(lon=slice(None, None, 12), lat=slice(None, None, 4)).\
plot.quiver('lon', 'lat', 'ug', 'vg', ax=ax, transform=ccrs.PlateCarree(), scale=1.5)
# show time step
ax.set_title(frame)
return im
# call animation function recursively
ani = FuncAnimation(fig, update, frames=sla_annual_mean.keys(), interval=200)
# ani = FuncAnimation(fig, update, frames=len(sla), interval=200)
plt.show()
####### why number 7.999.. appearing on the colorbar ??
####### plot time series of Barents Sea
# lat1 = 75.25
# lon1 = 37.5
# for i in range(2011, 2021):
# plt.plot(i, ds_velocity.sel(lon=lon1, lat=lat1).values, 'r-')
# # ds_velocity.sel(lon=lon1, lat=lat1).plot('lon', 'lat', 'sla')
# plt.show()