[ad_1]
I'm relatively inexperienced and I'm trying to plot some CFSR data. Specifically, I want to plot 250 hPa winds and heights, over a North Pacific domain. I am successfully able to plot the winds and height contours together. However, when I try and use plt.clabel to label the height contours, I get an error.
import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage as ndimage
plt.rcParams['figure.figsize'] = (12, 12)
datafile1= "/nfs/cfsr/data/2017/u.2017.0p5.anl.nc"
datafile2= "/nfs/cfsr/data/2017/v.2017.0p5.anl.nc"
datafile3="/nfs/cfsr/data/2017/g.2017.0p5.anl.nc"
data1=xr.open_dataset(datafile1)
data2=xr.open_dataset(datafile2)
data3=xr.open_dataset(datafile3)
print data3
lat = data3['lat']
lon = data3['lon']
#Time=Feb 3 00UTC, lev=250 hpa
u = data1['u'].isel(time=132).isel(lev=20)
v = data2['v'].isel(time=132).isel(lev=20)
g = data3['g'].isel(time=132).isel(lev=20)
wind=np.sqrt((u**2+v**2))
def plotMap():
#Set the projection information
proj = ccrs.Orthographic(central_longitude=-169,central_latitude=53)#, standard_parallels=[53])
#Create a figure with an axes object on which we will plot. Pass the projection to that axes.
fig, ax = plt.subplots(subplot_kw=dict(projection=proj))
#Zoom in
ax.set_extent([140, -105, 5, 60])
#Add map features
ax.add_feature(cfeature.LAND, facecolor='0.9') #Grayscale colors can be set using 0 (black) to 1 (white)
ax.add_feature(cfeature.LAKES, alpha=0.9) #Alpha sets transparency (0 is transparent, 1 is solid)
ax.add_feature(cfeature.BORDERS, zorder=10)
ax.add_feature(cfeature.COASTLINE, zorder=10)
states_provinces = cfeature.NaturalEarthFeature(
category='cultural', name='admin_1_states_provinces_lines',
scale='50m', facecolor='none')
ax.add_feature(states_provinces, edgecolor='gray', zorder=10)
#Add lat/lon gridlines every 20° to the map
ax.gridlines(xlocs=np.arange(0,361,20), ylocs=np.arange(-80,90,20))
return fig, ax
#Get a new background map figure
fig, ax = plotMap()
#Vector wind, using plasma colormap and orthographic proj
hght_levels = np.arange(9500,11250,250)
hght_contour=ax.contour(lon, lat, g, colors='k', levels=hght_levels, linewidths=1, zorder=3, transform = ccrs.PlateCarree())
wind_levels = np.linspace(0, 90, 10)
wind_contour = ax.contourf(lon, lat, wind, levels = wind_levels,
cmap=plt.cm.viridis, zorder=0.8, transform = ccrs.PlateCarree())
cb = plt.colorbar(wind_contour, shrink=0.5)
cb.set_ticklabels(wind_levels)
plt.clabel(hght_contour, hght_levels, inline=1, fmt='%1i', fontsize=12)
#Show the figure
fig
Finally, here is the error generated.
Error generated from code provided
Thanks in advance.
[ad_2]
لینک منبع