爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 8695|回复: 4

[求助] Python白化 maskout

[复制链接]

新浪微博达人勋

发表于 2022-3-17 13:36:53 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

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()


输出结果

输出结果

原始数据

原始数据
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2022-3-17 13:46:05 | 显示全部楼层
小图那块设置一下绘图范围
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2022-3-17 13:55:26 | 显示全部楼层
1099221723 发表于 2022-3-17 13:46
小图那块设置一下绘图范围

ax2.set_extent([105, 125, 0, 25])不就是设置了绘图范围嘛
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-3-17 18:26:39 | 显示全部楼层
溪午不闻 发表于 2022-3-17 13:55
ax2.set_extent([105, 125, 0, 25])不就是设置了绘图范围嘛

在画图后面设置
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-4-15 10:03:08 | 显示全部楼层
我也出现过这种情况,我是把小图的数据范围约束到和小图经纬度一样解决的
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表