Global Surface Temperature¶
In [1]:
Copied!
import numpy as np
import xarray as xr
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
ds = xr.open_dataset(r'./data/air.sfc.2021.nc')
lats = ds.variables['lat'][:]
lons = ds.variables['lon'][:]
time = ds.variables['time'][:]
temp = ds.variables['air'][:]
fig = plt.figure(figsize=(12,9))
m = Basemap(projection='cyl', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=0, urcrnrlon=360)
lon, lat = np.meshgrid(lons, lats)
x, y = m(lon, lat)
cs = m.contourf(x, y, temp[0, :, :], cmap='coolwarm', extend='both')
m.drawcoastlines()
m.drawparallels(np.arange(-90., 91., 30.), labels=[1, 0, 0, 0])
m.drawmeridians(np.arange(-180., 181., 60.), labels=[0, 0, 0, 1])
cbar = plt.colorbar(cs, orientation='horizontal', label='Surface Air Temperature (K)')
plt.title('Global Surface Temperature 2021 from NCEP')
plt.show()
import numpy as np
import xarray as xr
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
ds = xr.open_dataset(r'./data/air.sfc.2021.nc')
lats = ds.variables['lat'][:]
lons = ds.variables['lon'][:]
time = ds.variables['time'][:]
temp = ds.variables['air'][:]
fig = plt.figure(figsize=(12,9))
m = Basemap(projection='cyl', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=0, urcrnrlon=360)
lon, lat = np.meshgrid(lons, lats)
x, y = m(lon, lat)
cs = m.contourf(x, y, temp[0, :, :], cmap='coolwarm', extend='both')
m.drawcoastlines()
m.drawparallels(np.arange(-90., 91., 30.), labels=[1, 0, 0, 0])
m.drawmeridians(np.arange(-180., 181., 60.), labels=[0, 0, 0, 1])
cbar = plt.colorbar(cs, orientation='horizontal', label='Surface Air Temperature (K)')
plt.title('Global Surface Temperature 2021 from NCEP')
plt.show()
/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/xarray/conventions.py:551: SerializationWarning: variable 'air' has multiple fill values {-99, 32767}, decoding all values to NaN. new_vars[k] = decode_cf_variable(
In [2]:
Copied!
fig = plt.figure(figsize=(16, 12))
m = Basemap(projection='cyl', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=0, urcrnrlon=360)
for i in range(12):
month_temp = temp[i::12, :, :].mean(dim='time')
x, y = np.meshgrid(lons, lats)
ax = fig.add_subplot(4, 3, i+1)
cs = m.contourf(x, y, month_temp, cmap='coolwarm', extend='both')
m.drawcoastlines()
m.drawparallels(np.arange(-90., 91., 30.), labels=[1, 0, 0, 0])
m.drawmeridians(np.arange(-180., 181., 60.), labels=[0, 0, 0, 1])
ax.set_title(pd.to_datetime(time[i*31].values).strftime('%Y/%m'))
cbar = plt.colorbar(cs, ax=ax, orientation='horizontal', label='Surface Air Temperature (K)')
cbar.ax.tick_params(labelsize=8)
plt.suptitle('Seasonal Variation of Global Surface Temperature 2021 from NCEP', fontsize=16)
plt.tight_layout()
plt.show()
fig = plt.figure(figsize=(16, 12))
m = Basemap(projection='cyl', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=0, urcrnrlon=360)
for i in range(12):
month_temp = temp[i::12, :, :].mean(dim='time')
x, y = np.meshgrid(lons, lats)
ax = fig.add_subplot(4, 3, i+1)
cs = m.contourf(x, y, month_temp, cmap='coolwarm', extend='both')
m.drawcoastlines()
m.drawparallels(np.arange(-90., 91., 30.), labels=[1, 0, 0, 0])
m.drawmeridians(np.arange(-180., 181., 60.), labels=[0, 0, 0, 1])
ax.set_title(pd.to_datetime(time[i*31].values).strftime('%Y/%m'))
cbar = plt.colorbar(cs, ax=ax, orientation='horizontal', label='Surface Air Temperature (K)')
cbar.ax.tick_params(labelsize=8)
plt.suptitle('Seasonal Variation of Global Surface Temperature 2021 from NCEP', fontsize=16)
plt.tight_layout()
plt.show()
In [3]:
Copied!
# Add the line plot
time_values = pd.to_datetime(time.values)
plt.figure(figsize=(12, 6))
plt.plot(time_values, temp.mean(dim=('lat', 'lon')), label='Global Average Temperature')
plt.xlabel('Time')
plt.ylabel('Temperature (K)')
plt.title('Global Average Surface Temperature 2021 from NCEP')
plt.legend()
plt.grid()
plt.show()
# Add the line plot
time_values = pd.to_datetime(time.values)
plt.figure(figsize=(12, 6))
plt.plot(time_values, temp.mean(dim=('lat', 'lon')), label='Global Average Temperature')
plt.xlabel('Time')
plt.ylabel('Temperature (K)')
plt.title('Global Average Surface Temperature 2021 from NCEP')
plt.legend()
plt.grid()
plt.show()
In [ ]:
Copied!