Matplotlib 用cartopy绘制python-xarray

Matplotlib 用cartopy绘制python-xarray,matplotlib,python-xarray,cartopy,Matplotlib,Python Xarray,Cartopy,我想使用与xarray关联的cartopy绘图功能。但是,我不明白为什么轮廓与单机版相比有所不同cartopy。我也不知道如何传递plt.colorbar的设置 我有一个带有空间变化的NAN的文件,绘图函数似乎很难解决这个问题。我只是希望了解绘图功能是如何工作的,为什么它与 这是我的密码: import numpy as np import xarray as xr import matplotlib.pyplot as plt import matplotlib.ticker as mtick

我想使用与
xarray
关联的
cartopy
绘图功能。但是,我不明白为什么轮廓与单机版相比有所不同
cartopy
。我也不知道如何传递
plt.colorbar
的设置

我有一个带有空间变化的NAN的文件,绘图函数似乎很难解决这个问题。我只是希望了解绘图功能是如何工作的,为什么它与

这是我的密码:

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy.crs as ccrs  
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.feature as cfeature

modellist = ['CCSM4'] # Choose from CCSM4 (1982-2015), CanCM4 (1981-2011), CanCM3 (1981-2011), CFSV2 (1982-2010), GloSea5 (1994-2015 (no 09ic)), System4 (1994-2015), System5 (1993-2014)
modelsave = "".join(modellist)
observation = 'ERAI'    
years_arr=range(1994,2011)    
ic = '12' # 09, 10, 11, 12
varstr = 'ws10'
timeavg = 'DJFmean'
skillmetric = 'rmse'
if varstr == 'ws10': 
    units = r'm $s^{-1}$'

# Download the netCDF file from 
# https://miami.box.com/s/m80adf8dww1aslzb7g6louwzhvujsr54
filein = skillmetric + '_' + ic +'ic_' + modelsave + '_' + observation + '_' + varstr + '_' + timeavg + '_' + str(years_arr[0]) + '-' + str(years_arr[-1]) + '.nc'
da = xr.open_dataarray(filein)    
rmse = da.values

# 2D plot doesn't really work. See plot at http://imgur.com/a/J3TMQ
da.plot()

# Some plot settings:
lon = da.coords['longitude'].values
lat = da.coords['latitude'].values
lon180 = urb.lonto180(lon)
cen_lat = (lat[-1] - lat[0]) / 2.0
cen_lon = ((abs(lon180[-1]) + abs(lon180[0])) / 2.0) + lon180[0]
lonticks = np.arange(lon180[0], lon180[-1]+0.01, 20.0)
latticks = np.arange(lat[0], lat[-1]+0.01, 10.0)
clevsmean = np.linspace(0, 2, 64)
mean_ticks = np.arange(0, 2.01, 0.5) 

# Try to call cartopy from the DataArray
# see plot at http://imgur.com/a/R7sI4
# Some issues with contour e.g. meridian and Pacific
# I also couldn't work out how to edit the colorbar
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=cen_lon)) 
ax.set_extent([lon180[0]-0.01, lon180[-1]+0.01, lat[0]-0.01, lat[-1]+0.01], crs=ccrs.PlateCarree())
ax.coastlines(resolution='50m')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, alpha=0)
gl.xlabels_top = False
gl.ylabels_right = False
gl.xlocator = mticker.FixedLocator(lonticks)
gl.ylocator = mticker.FixedLocator(latticks)
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER 
da.plot.contourf(ax=ax, levels=clevsmean, transform=ccrs.PlateCarree(), cmap='jet')
#cbar = plt.colorbar(p=da, ticks=mean_ticks)
ax.add_feature(cfeature.LAND, facecolor='black')
plt.title(modelsave + ' ' + ic + 'ic - ' + observation + ' ' + timeavg + ' ' + str(years_arr[0]) 
          + '-' + str(years_arr[-1]) + ' \n' + varstr + ' ' + skillmetric, fontsize=8)    

# Cartopy plot using the np.darray
# See plot at http://imgur.com/a/A9D58
plt.figure(figsize=(5.12985642927, 3))
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=cen_lon))   
ax.set_extent([lon180[0]-0.01, lon180[-1]+0.01, lat[0]-0.01, lat[-1]+0.01], crs=ccrs.PlateCarree())
ax.coastlines(resolution='50m')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, alpha=0)
gl.xlabels_top = False
gl.ylabels_right = False
gl.xlocator = mticker.FixedLocator(lonticks)
gl.ylocator = mticker.FixedLocator(latticks)
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
p = ax.contourf(lon180, lat, rmse, clevsmean, transform=ccrs.PlateCarree(), cmap='jet')
ax.add_feature(cfeature.LAND, facecolor='black')
cbar = plt.colorbar(p, ticks=mean_ticks)
cbar.set_label(units)
plt.title(modelsave + ' ' + ic + 'ic - ' + observation + ' ' + timeavg + ' ' + str(years_arr[0]) 
          + '-' + str(years_arr[-1]) + ' \n' + varstr + ' ' + skillmetric, fontsize=8)

问题是什么?问题是什么?