Sha*_*ban 5 python statistics matplotlib contour anova
我正在尝试在等高线图上添加阴影线(如点、散列等)。这种阴影线可以代表唯一具有统计显着性的轮廓,或者具有某些标准的轮廓。就像下面关于自然文章的图片(第二个和第三个图)http://www.nature.com/articles/srep16853/figures/3。
\n\n以下代码显示了来自 NOAA 数据的降水图,可在以下位置下载。
\n\nimport numpy as np\nimport sys \nimport netCDF4 as nc\nimport matplotlib.pyplot as plt\nimport matplotlib.mlab as m \nimport mpl_toolkits.basemap as bm\nimport os\nsys.path.insert(0, \'../\');import py4met as sm;reload(sm)\n\n#- Reading data for a timeslice, latitude, and longitude:\ndiri_output="./"\ndiri="./"\ntmp_file = nc.Dataset(diri+"precip.mon.mean.nc","r")\nprint(tmp_file.variables)\np_pre = tmp_file.variables[\'precip\']\nlat = tmp_file.variables[\'lat\'][:]\nlon = tmp_file.variables[\'lon\'][:]\ntime = tmp_file.variables[\'time\']\ntmp_file.close\n\n\nlat1=np.min(lat)\nlat2=np.max(lat)\nlon1=np.min(lon)\nlon2=np.max(lon)\n\n[lonall, latall] = np.meshgrid(lon[:], lat[:])\nplt.figure(num=None, figsize=(8+4, 6+4), dpi=80, facecolor=\'w\', edgecolor=\'k\') \nmapproj = bm.Basemap(projection=\'cyl\',llcrnrlat=lat1, llcrnrlon=lon1,urcrnrlat=lat2, urcrnrlon=lon2,resolution=\'l\')\nmapproj.drawcoastlines()\nmapproj.drawmapboundary(fill_color=\'white\')\nmapproj.drawcountries()\nx, y = mapproj(lonall, latall)\nplt.contourf(x,y,p_pre[240,:,:],cmap=plt.cm.GnBu)\nplt.colorbar(orientation=\'horizontal\',pad=0.05,shrink=0.6)\nplt.title("title")\nxx,yy=np.where(p_pre[240,:,:] >= 20)\nsig=np.copy(p_pre[0,:,:])\nsig[:,:]=1\nsig[xx,yy]=0\n#plt.contourf(x,y,sig,hatches=[\'.\'])\nplt.show() \n
Run Code Online (Sandbox Code Playgroud)\n\n我想孵化所有20毫米以上的轮廓,所以我使用了上面的命令
\n\n\n\n\nplt.contourf(x,y,sig,hatches=[\'.\'])
\n
但它没有\xe2\x80\x99工作(它在地图上到处都是点,而不仅仅是具有特定标准的轮廓),因此我评论了它。\n任何想法。
\n请参阅此matplotlib 示例页面,了解如何将剖面线与 一起使用的演示contourf
。与您的问题特别相关的是(1)有一个关键字level
,contourf
用于确定着色和/或阴影线的值的范围,(2)""
可以使用空字符串来表示没有阴影线。
因此,不要plt.contourf
尝试注释掉那行
levels = [p_pre[240,:,:].min(), 20, p_pre[240,:,:].max()]
plt.contourf(x, y, p_pre[240,:,:], levels=levels, hatches=["", "."], alpha=0)
Run Code Online (Sandbox Code Playgroud)
我无法从您链接到的数据重新创建您的绘图,因此我生成了一些随机数据,以使用我上面描述的相同原理制作下面的图像。