我有一张1979年至2014年在南极洲周围平均海冰浓度的地图,我在那里设定了陆地上的数值到NaN(它们原本是-1e30,因为很明显你不能在陆地上有海冰值)。由于分辨率,沿着海岸的一些NaN块延伸到海洋的一部分,而奇怪的是它们不是留白,它们被重新填充,好像它们的值是颜色条所允许的最大负值。当我绘制没有底图的数据时,它是颠倒的,但NaN未被填充。
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap, cm
%matplotlib inline
file = Dataset("HadISST_ice.nc")
ice = file.variables['sic'][1309:1737] #Feb 1979-Sept 2014
lat = file.variables['latitude'][:]
lon = file.variables['longitude'][:]
print ice.shape
输出:
(428, 180, 360)
ice_filt = np.asarray(ice) #copies data to new array
missing_values_indices = ice_filt < -8000000 #finds where values are below threshold
ice_filt[missing_values_indices] = np.nan #changes values below threshold to NaN
print ice[0,90,125], ice_filt[0,90,125]
输出:
-- nan
febice = np.zeros((35,180,360))
a=0
for i in np.arange(35):
febice[i,:,:] = ice_filt[a,:,:]
a = a+12
febice_mean = np.nanmean(febice,axis=0)
#febice_mean = np.flipud(febice_mean)
print febice_mean.shape
输出:
(180, 360)
plt.contourf(febice_mean)
plt.colorbar()
fig = plt.figure(figsize=(12,14))
m = Basemap(projection='spstere',lon_0=270,boundinglat=-55.,resolution='l') #create basemap & specify res
m.drawcoastlines(color='grey')
m.fillcontinents(color='0.8')
m.drawcountries()
meshlon,meshlat = np.meshgrid(lon,lat)
x, y = m(meshlon,meshlat) #assign 2D arrays to new variables
sic = m.pcolormesh(x,y,febice_mean,cmap=plt.cm.Blues_r,vmin=0.,vmax=1)
cbar=m.colorbar(sic,size="3%",)
cbar.set_label('Sea ice fractional coverage')
plt.title('Antarctic Sea Ice Cover (1979-2014 February Mean)',fontsize=16)
plt.show()
PS:我知道NaN填充在这里看起来很挑剔,但它更加引人注目。当我为北极绘图时,有点分散注意力。