有的时候,我们在绘制完图形后,图形中会出现白条
类似如下:
代码为:
nc_file=Dataset('C:/Users/86132/sst.mon.mean.nc')
lon=nc_file.variables['lon'][:]#读取经度
lat=nc_file.variables['lat'][:]#读取纬度
sst=nc_file.variables['sst'][0][:][:]#读取0时刻海温
fig = plt.figure(figsize=(8,6),dpi=500)#创建画板
ax=fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.coastlines()#添加海岸线
ax.add_feature(cfeat.LAND)#添加陆地
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=0.7, color='k', alpha=1, linestyle='--')#网格线
gl.xlabels_top = False # 关闭顶端的经纬度标签
gl.ylabels_right = False # 关闭右侧的经纬度标签
gl.xformatter = LONGITUDE_FORMATTER # x轴设为经度的格式
gl.yformatter = LATITUDE_FORMATTER # y轴设为纬度的格式
gl.xlocator = mticker.FixedLocator(np.arange(-180, 180.1, 30))
gl.ylocator = mticker.FixedLocator(np.arange(-90,90.1, 30))
gl.xlines=False#关闭内部经纬线
gl.ylines=False#关闭内部经纬线
ax.contourf(lon,lat,sst,levels=np.arange(-10,35),cmap='RdBu_r')
plt.savefig('海温场',bbox_inches='tight')
plt.show()
解决办法为:
利用meshgrid添加数据循环,进而防止白条的出现。
nc_file=Dataset('C:/Users/86132/sst.mon.mean.nc')
lon=nc_file.variables['lon'][:]#读取经度
lat=nc_file.variables['lat'][:]#读取纬度
sst=nc_file.variables['sst'][0][:][:]#读取0时刻海温
fig = plt.figure(figsize=(8,6),dpi=500)#创建画板
ax=fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.coastlines()#添加海岸线
ax.add_feature(cfeat.LAND)#添加陆地
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=0.7, color='k', alpha=1, linestyle='--')#网格线
gl.xlabels_top = False # 关闭顶端的经纬度标签
gl.ylabels_right = False # 关闭右侧的经纬度标签
gl.xformatter = LONGITUDE_FORMATTER # x轴设为经度的格式
gl.yformatter = LATITUDE_FORMATTER # y轴设为纬度的格式
gl.xlocator = mticker.FixedLocator(np.arange(-180, 180.1, 30))
gl.ylocator = mticker.FixedLocator(np.arange(-90,90.1, 30))
gl.xlines=False#关闭内部经纬线
gl.ylines=False#关闭内部经纬线
#################以下步骤添加数据循环,防止白条##################
cycle_sst, cycle_lon = add_cyclic_point(sst, coord=lon)
cycle_LON, cycle_LAT = np.meshgrid(cycle_lon, lat)
ax.contourf(cycle_LON,cycle_LAT,cycle_sst,levels=np.arange(-10,35),cmap='RdBu_r')
plt.savefig('海温场',bbox_inches='tight')
plt.show()