python - python - 如何使用Basemap lib在Python中的contourf上绘制点?
问题描述
我正在寻找一种简单的方法来在轮廓底图图中的某些值上绘制点
这是一个示例代码:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
dx, dy = 0.5, 0.5
# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(-15, 15 + dy, dy),
slice(-90, -60 + dx, dx)]
z = np.sin(x) + np.cos(5 + y*x) * np.cos(x) + x*y/100
n, m = z.shape
z = z - np.exp(np.random.rand(n,m))
#Setup the map
m = Basemap(projection='merc', llcrnrlat=-15, urcrnrlat=15,\
llcrnrlon=-90, urcrnrlon=-60, resolution='l')
m.drawcoastlines()
xx, yy = m(x,y)
cs = m.contourf(xx,yy,z,cmap=plt.cm.Spectral_r)
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5)
plt.show()
我想要a = np.where(abs(z) > 5,np.nan,z)
Basemap 像附图中那样绘制点的地方
解决方案
您可以在第二次调用中使用舱口盖contourf()
:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
dx, dy = 0.5, 0.5
# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(-15, 15 + dy, dy),
slice(-90, -60 + dx, dx)]
z = np.sin(x) + np.cos(5 + y*x) * np.cos(x) + x*y/100
n, m = z.shape
z = z - np.exp(np.random.rand(n,m))
#Setup the map
m = Basemap(projection='merc', llcrnrlat=-15, urcrnrlat=15,\
llcrnrlon=-90, urcrnrlon=-60, resolution='l')
m.drawcoastlines()
xx, yy = m(x,y)
cs = m.contourf(xx,yy,z,cmap=plt.cm.Spectral_r)
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5)
##adding hatches:
cs = m.contourf(xx, yy, z, levels=[np.min(z),5,np.max(z)], colors='none',
hatches=[None,'.',],
extend='lower')
plt.show()
给出以下图像:
推荐阅读
- javascript - 使用lodash在javascript中查找两组数据之间的匹配
- r - DateTime 到 R 中的天数并找到平均值
- php - 无法在版本 7.2 UBUNTU 上安装 PHP GD
- mysql - 如何从mysql中的当前日期获取明年
- html - 诡异的
- Microsoft Edge 上带有“显示:表”子元素的高度错误
- python - 从特定表解析 Python BeautifulSoup
- xml - 递归键值对的 XML 模式
- eclipse - 如何将 Kotlin 添加到 Eclipse 中的现有 Java 项目中?
- java - java - 如何在java解析器中插入以字符串格式给出的语句?
- c# - C# 从列表框中获取项目并从网络搜索