- 积分
- 2415
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-11-25
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2015-7-17 10:16:43
|
显示全部楼层
本帖最后由 po_po1 于 2015-7-17 10:18 编辑
利用cartopy绘图:
这是模式初始场的植被发布图,图中号码为区域植被类型代码
绘图代码如下:
from netCDF4 import Dataset
​
q=Dataset(r'D:/Desktop/ngc_DOMAIN000.nc')
xlon=q.variables['xlon'][:]
xlat=q.variables['xlat'][:]
lad=q.variables['landuse'][:]
​
​
import matplotlib.pyplot as plt
import numpy as np
import cartopy.io.shapereader as shpreader
from cartopy.mpl.patch import geos_to_path
from matplotlib.patches import PathPatch
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import shapely.geometry as sgeom
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER , LATITUDE_FORMATTER
import matplotlib.colors as mc
def myset(ax):
plate_carre_data_transform = ccrs.PlateCarree()._as_mpl_transform(ax)
line=ax.gridlines(draw_labels=True,alpha=0.5,linestyle='--')
line.xlabels_top=False
line.ylabels_left=False
#line.xlines=False#只剩下x轴平行线
line.xlocator=mticker.FixedLocator(np.arange(70,140,10))#手动设置x轴刻度
line.xformatter= LONGITUDE_FORMATTER
line.yformatter= LATITUDE_FORMATTER
line.xlabel_style={'size':10,'color':'black'}
line.ylabel_style={'size':10,'color':'black'}
line.yalbel_style={'color':'red','weight':'bold'}
return
​
​
def mycon(ax,level,level0,pre,x,y,cmape):
plate_carre_data_transform = ccrs.PlateCarree()._as_mpl_transform(ax)
file1=r'D:/baiduyundownload/map/China boundary/province boundary/bou2_4p.shp'
reader1 = shpreader.Reader(file1).geometries()
for province in reader1:
ax.add_geometries([province],ccrs.PlateCarree(),linewidth=0.4,\
edgecolor='black',facecolor='none',alpha=0.75)
norm= mc.BoundaryNorm(level, 256)
quad_set = plt.contourf(xlon,xlat,lad,alpha=0.75,levels=level,norm=norm, \
cmap=cmape,transform=ccrs.PlateCarree())#绘制等值线填充
plt.colorbar(quad_set,orientation='horizontal',ticks=level0,\
shrink=0.85,extend='both',extendrect=False,pad=0.05)
def mlable(ax,level,level0,pre,x,y,cmape):
plate_carre_data_transform = ccrs.PlateCarree()._as_mpl_transform(ax)
cs=plt.contour(x,y,pre,alpha=0.75,levels=level,colors='w',\
linewidths=0.02,\
transform=ccrs.PlateCarree())
cl=plt.clabel(cs,level,fmt='%i',colors='black',\
fontsize=12,manual=True)
for cp in cl:
cp.set_rotation('horizontal')
cp.set_bbox(dict(facecolor='white', edgecolor='w', alpha=0.5))#,\
cp.update_bbox_position_size=5
​
level=range(0,20);level0=range(1,20)
cmap='Dark2'
​
ax=plt.axes([0.02,0.02,0.9,0.94],projection=ccrs.PlateCarree())
myset(ax)
mycon(ax,level,level0,lad,xlon,xlat,cmap)
mlable(ax,level,level0,lad,xlon,xlat,cmap)
​
plt.show()
​
​
|
|