将颜色条范围调整为底图等高线图的可见部分

时间:2015-08-28 12:39:14

标签: matplotlib colorbar matplotlib-basemap contourf

我在底图上有一个等高线图,我希望调整颜色条的范围,使它们适合可见数据。默认设置使颜色范围适合所有数据,即那些未绘制的数据。有这个设置吗?

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid

myllcrnrlat=35
myurcrnrlat=65
myllcrnrlon=-45
myurcrnrlon=45
m = Basemap(projection='cyl', llcrnrlat=myllcrnrlat, urcrnrlat=myurcrnrlat,\
llcrnrlon=myllcrnrlon, urcrnrlon=myurcrnrlon, resolution='c')

lonsin=np.asarray(range(0,360,10))
latsin=np.asarray(range(-90,90,10))
valin=np.random.rand(len(latsin), len(lonsin))
valin[0,0]=5  #this is a value outside my basemap area and higher than all inside.
valin_cyclic, lons_cyclic = addcyclic(valin, lonsin)
valin_cyclic, lons_cyclic = shiftgrid(180., valin_cyclic, lons_cyclic, start=False)
lon2d, lat2d = np.meshgrid(lons_cyclic, latsin)
x, y = m(lon2d, lat2d)

cs = m.pcolormesh(x, y, valin_cyclic,cmap=plt.get_cmap('autumn_r'))
cbar = plt.colorbar(cs)
plt.show()

我可以通过做类似下面的事情来使用vmin,vmax,但它似乎相当长,所以也许有一个特殊的设置?

lonsin_inbasemap=np.asarray([a for a in lonsin if myllcrnrlon <= a if a <= myurcrnrlon])
latsin_inbasemap=np.asarray([a for a in latsin if myllcrnrlat <= a if a <= myurcrnrlat])
valin_inbasemap_tmp = np.transpose(np.asarray([valin[:,a] for a in range(len(lonsin)) if lonsin[a] in lonsin_inbasemap]))
valin_inbasemap     = np.asarray([valin_inbasemap_tmp[a,:] for a in range(len(latsin)) if latsin[a] in latsin_inbasemap])
del(valin_inbasemap_tmp)

vmax=np.amax(valin_inbasemap)
cs = m.pcolormesh(x, y, valin_cyclic,vmax=vmax, cmap=plt.get_cmap('autumn_r'))
cbar = plt.colorbar(cs)
plt.show()

1 个答案:

答案 0 :(得分:0)

如果要屏蔽某些低于某个值的数据 例如,您不希望显示的减号数据: 你可以使用``

valin = np.ma.masked_less(valin_cyclic,0)
cmap1 = plt.cm,get_cmap("autumn_r")
cmap1.set_bad("w")

p =plt.pcolor((x, y,conc,cmap=cmap1,alpha =1,zorder =2)