如何使用底图lib中绘制了contourf点在Python?

问题描述 投票:0回答:1

我正在寻找一种简单的方法来绘制超过一定值点在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)底图地块点只是图等安装

python matplotlib-basemap
1个回答
1
投票

您可以在第二次调用hatches使用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()

给出了下面的图片:

result of the above code

© www.soinside.com 2019 - 2024. All rights reserved.