![](/img/trans.png)
[英]How to draw contourf plot for a particular shape in Python-Matplotlib-Basemap
[英]How to plot dots over contourf in Python using Basemap lib?
我正在尋找一種在等高線底圖圖中將點繪制到特定值上的簡單方法
這是示例代碼:
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)
底圖繪制點的情況下,如附圖所示
您可以在第二次調用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()
給出以下圖像:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.