- 积分
- 750
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2021-2-24
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
各位大佬,本人在画图时遇到这样一个问题,由于数据包含了中国以外的部分,所以需要白化,使用的方法是参考家园中大神给出的maskout文件。但在实际过程中,在添加九段线的时候出现了意料之外的问题,在九段线上又出现了一个中国地图,想请各位帮忙看看是什么原因呢。谢谢大家。代码如下:
#import cartopy.feature as cfeat
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
import matplotlib.pyplot as plt
import xarray as xr
import cartopy.mpl.ticker as cticker
import maskout
import cartopy.io.shapereader as shpreader
t = xr.open_dataset('C:/Users/yu/Desktop/data/1961_2018_txn.nc')
lat = t.lat
lon = t.lon
TXn = t.TXn
TXn_mean = TXn.mean('year')
fig = plt.figure(figsize=(12,8),dpi=500)
china = shpreader.Reader('C:/Users/yu/Downloads/cartopy/china_shp/cnhimap.shp').geometries()
ax1 = fig.add_axes([0.5, 0.1, 0.8, 0.4],projection = ccrs.PlateCarree()) # [*left*, *bottom*, *width*,*height*]
c1 = ax1.contourf(lon,lat, TXn_mean,levels =np.arange(-30,10) ,extend='both', transform=ccrs.PlateCarree(), cmap=plt.cm.bwr)
clip = maskout.shp2clip(c1,ax1,r'C:\Users\yu\china_country','CHINA') #使用maskout
reader = Reader(r"china_country.shp")
china_country = cfeature.ShapelyFeature(reader.geometries(), ccrs.PlateCarree(), edgecolor='black', facecolor='none')
ax1.add_feature(china_country, linewidth=0.7)
ax1.set_xticks([60, 90, 120, 150])
ax1.set_xticklabels([u'60\N{DEGREE SIGN}E',u'90\N{DEGREE SIGN}E',u'120\N{DEGREE SIGN}E',u'150\N{DEGREE SIGN}E'])
ax1.set_yticks([ 20, 30, 40, 50, 60])
ax1.set_yticklabels([u'20\N{DEGREE SIGN}N',u'30\N{DEGREE SIGN}N',u'40\N{DEGREE SIGN}N',u'50\N{DEGREE SIGN}N',u'60\N{DEGREE SIGN}N'])
ax1.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=1, zorder=1)
lon_formatter = cticker.LongitudeFormatter()
lat_formatter = cticker.LatitudeFormatter()
ax1.set_title('(g) TXn_1961_2018',loc='left',fontsize=10)
ax1.set_title('℃',loc='right',fontsize=10)
fig.colorbar(c1,orientation='horizontal',aspect=40, shrink=0.4, pad=0.1,format='%d',)
#添加南海九道线
ax2 = fig.add_axes([1.031, 0.2, 0.087, 0.09], projection=ccrs.PlateCarree())
#ax2.add_feature(cfeat.COASTLINE.with_scale('50m'), linewidth=0.6, zorder=10) # 加载分辨率为50的海岸线
#ax2.add_feature(cfeat.RIVERS.with_scale('50m'), zorder=10) # 加载分辨率为50的河流
#ax2.add_feature(cfeat.LAKES.with_scale('50m'), zorder=10) # 加载分辨率为50的湖泊
ax2.set_extent([105, 125, 0, 25])
reader2 = Reader(r'C:\Users\yu\china_country')
china_country = cfeature.ShapelyFeature(reader2.geometries(), ccrs.PlateCarree(), edgecolor='black', facecolor='none')
ax2.add_feature(china_country, linewidth=0.5)
reader3 = Reader(r'C:\Users\yu\china_nine_dotted_line')
china_nine = cfeature.ShapelyFeature(reader3.geometries(), ccrs.PlateCarree(), edgecolor='black', facecolor='none')
ax2.add_feature(china_nine, linewidth=0.5)
c2= ax2.contourf(lon,lat,TXn_mean,levels =np.arange(-30,10),transform=ccrs.PlateCarree(), cmap=plt.cm.bwr )
clip2 = maskout.shp2clip(c2,ax2,r'C:\Users\yu\china_country','CHINA') #这里也使用maskout 但出图的时候多了一个小的中国地图
plt.show()
|
-
输出结果
-
原始数据
|