【问题标题】:overly patches which represent the significants points over contour map表示等高线图上的重要点的过度补丁
【发布时间】:2016-04-19 14:52:18
【问题描述】:

我正在尝试在等高线图上添加阴影(如点、散列、..)。这种影线可以代表唯一具有统计意义的等高线,或具有某些标准的等高线。如下图自然文章(第二和第三个情节)http://www.nature.com/articles/srep16853/figures/3

以下代码显示来自 NOAA 数据available 的降水图,供下载。

import numpy as np
import sys 
import netCDF4 as nc
import matplotlib.pyplot as plt
import matplotlib.mlab as m 
import mpl_toolkits.basemap as bm
import os
sys.path.insert(0, '../');import py4met as sm;reload(sm)

#- Reading data for a timeslice, latitude, and longitude:
diri_output="./"
diri="./"
tmp_file = nc.Dataset(diri+"precip.mon.mean.nc","r")
print(tmp_file.variables)
p_pre   = tmp_file.variables['precip']
lat     = tmp_file.variables['lat'][:]
lon     = tmp_file.variables['lon'][:]
time    = tmp_file.variables['time']
tmp_file.close


lat1=np.min(lat)
lat2=np.max(lat)
lon1=np.min(lon)
lon2=np.max(lon)

[lonall, latall] = np.meshgrid(lon[:], lat[:])
plt.figure(num=None, figsize=(8+4, 6+4), dpi=80, facecolor='w', edgecolor='k')    
mapproj = bm.Basemap(projection='cyl',llcrnrlat=lat1, llcrnrlon=lon1,urcrnrlat=lat2, urcrnrlon=lon2,resolution='l')
mapproj.drawcoastlines()
mapproj.drawmapboundary(fill_color='white')
mapproj.drawcountries()
x, y = mapproj(lonall, latall)
plt.contourf(x,y,p_pre[240,:,:],cmap=plt.cm.GnBu)
plt.colorbar(orientation='horizontal',pad=0.05,shrink=0.6)
plt.title("title")
xx,yy=np.where(p_pre[240,:,:] >= 20)
sig=np.copy(p_pre[0,:,:])
sig[:,:]=1
sig[xx,yy]=0
#plt.contourf(x,y,sig,hatches=['.'])
plt.show()  

我想对20毫米以上的所有轮廓进行孵化,所以我使用了上面的命令

plt.contourf(x,y,sig,hatches=['.'])

但它没有用(它在地图上的任何地方都画了点,而不仅仅是具有特定标准的轮廓),因此我评论了它。 任何想法。

【问题讨论】:

    标签: python matplotlib statistics contour anova


    【解决方案1】:

    请参阅此matplotlib example page,了解如何将舱口与contourf 一起使用的演示。与您的问题特别相关的是 (1) 有一个关键字 level contourf 用于确定哪些值被着色和/或阴影的界限和 (2) 一个空字符串 "" 可以是用于没有舱口。

    所以,你注释掉了 try 而不是 plt.contourf

    levels = [p_pre[240,:,:].min(), 20, p_pre[240,:,:].max()]
    plt.contourf(x, y, p_pre[240,:,:], levels=levels, hatches=["", "."], alpha=0)
    

    我无法根据您链接到的数据重新创建您的绘图,因此我生成了一些随机数据,使用我上面描述的相同原理制作下面的图像。

    【讨论】:

    • 非常感谢,它对我有用。现在,我知道使用的阴影数是使用的线数-1,因此我可以用不同的阴影孵化不同的轮廓级别。再次感谢。
    • 我很高兴 matplotlib 有这样的功能,我搜索了如何在 MatLab 中进行修补,但没有找到这样的内置功能,而是在 MatLab 文件交换link 上找到了以下贡献。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多