Chr*_*neB 7 python arrays matplotlib colorbar jupyter-notebook
问题
因此,我有一个包含6层(array.size = (192,288,6))的lat-lon数组,其中包含一堆数据,其值范围从近0到到0.65。当我从每个6图层([:,:,0],[:,:,1]等)绘制数据时,除了,我都没有问题并得到一张漂亮的地图[:,:,4]。出于某种原因,当我尝试绘制此2D数组时,收到一条我不理解的错误消息,并且仅当我尝试包含色条时才会出现。如果我不使用颜色条,就没有错误,但是我需要颜色条...
代码
这是我用于数组其他部分的代码以及生成的图。我们一起去吧[:,:,5]。
#Set labels
lonlabels = ['0','45E','90E','135E','180','135W','90W','45W','0']
latlabels = ['90S','60S','30S','Eq.','30N','60N','90N']
#Set cmap properties
bounds = np.array([0,0.001,0.01,0.05,0.1,0.2,0.3,0.4,0.5,0.6])
boundlabels = ['0','0.001','0.01','0.05','0.1','0.2','0.3','0.4','0.5','0.6']
cmap = plt.get_cmap('jet')
norm = colors.PowerNorm(0.35,vmax=0.65) #creates logarithmic scale
#Create basemap
fig,ax = plt.subplots(figsize=(15.,10.))
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,llcrnrlon=0,urcrnrlon=360.,lon_0=180.,resolution='c')
m.drawcoastlines(linewidth=2,color='w')
m.drawcountries(linewidth=2,color='w')
m.drawparallels(np.arange(-90,90,30.),linewidth=0.3)
m.drawmeridians(np.arange(-180.,180.,45.),linewidth=0.3)
meshlon,meshlat = np.meshgrid(lon,lat)
x,y = m(meshlon,meshlat)
#Plot variables
trend = m.pcolormesh(x,y,array[:,:,5],cmap='jet',norm=norm,shading='gouraud')
#Set plot properties
#Colorbar
cbar=m.colorbar(trend, size='5%',ticks=bounds,location='bottom',pad=0.8)
cbar.set_label(label='Here is a label',size=25)
cbar.set_ticklabels(boundlabels)
for t in cbar.ax.get_xticklabels():
t.set_fontsize(25)
#Titles & labels
ax.set_title('Here is a title for [:,:,5]',fontsize=35)
ax.set_xlabel('Longitude',fontsize=25)
ax.set_xticks(np.arange(0,405,45))
ax.set_xticklabels(lonlabels,fontsize=20)
ax.set_yticks(np.arange(-90,120,30))
ax.set_yticklabels(latlabels,fontsize=20)
Run Code Online (Sandbox Code Playgroud)
现在,当我使用完全相同的代码但为array[:,:,4]而不是进行绘制时array[:,:,5],出现此错误。
ValueError Traceback (most recent call last)
/linuxapps/anaconda/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
[lots of further traceback]
/linuxapps/anaconda/lib/python2.7/site-packages/matplotlib/text.pyc in draw(self, renderer)
755 posy = float(textobj.convert_yunits(textobj._y))
756 if not np.isfinite(posx) or not np.isfinite(posy):
--> 757 raise ValueError("posx and posy should be finite values")
758 posx, posy = trans.transform_point((posx, posy))
759 canvasw, canvash = renderer.get_canvas_width_height()
ValueError: posx and posy should be finite values
Run Code Online (Sandbox Code Playgroud)
我不知道为什么要这么做,因为我对阵列图的其他每个部分的代码都很好,而且它们都使用相同的网格。NaN数组中没有。如果我将#Colorbar和之间的所有代码都注释掉,这也是结果#Titles & labels
更新:当我包含颜色条码但将其更改PowerNorm为1.0(norm = colors.PowerNorm(1.0,vmax=0.65))时,问题也消失了。1.0包括颜色条时,除生成错误以外,其他所有操作均不发生。
问题
可能是什么原因导致posx&posy错误消息,如何消除该错误消息,以便可以使用包含颜色栏的图表进行绘制?
更新
当我从头开始运行内核时,再次使用相同的代码(除了将0.6绑定更改为0.65),在该array[:,:,4]块中收到以下警告。我不确定它们是否相关,但是为了以防万一,我将它们包括在内。
/linuxapps/anaconda/lib/python2.7/site-packages/matplotlib/colors.py:1202: RuntimeWarning: invalid value encountered in power
np.power(resdat, gamma, resdat)
[<matplotlib.text.Text at 0x2af62c8e6710>,
<matplotlib.text.Text at 0x2af62c8ffed0>,
<matplotlib.text.Text at 0x2af62cad8e90>,
<matplotlib.text.Text at 0x2af62cadd3d0>,
<matplotlib.text.Text at 0x2af62caddad0>,
<matplotlib.text.Text at 0x2af62cae7250>,
<matplotlib.text.Text at 0x2af62cacd050>]
/linuxapps/anaconda/lib/python2.7/site-packages/matplotlib/axis.py:1015: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
/linuxapps/anaconda/lib/python2.7/site-packages/matplotlib/axis.py:1025: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed.
warnings.warn("Unable to find pixel distance along axis "
Run Code Online (Sandbox Code Playgroud)
因此,我发现指定vmax并vmin解决了问题。我不知道为什么,但是一旦这样做,我的图就可以通过色标正确显示出来。
trend = m.pcolormesh(x,y,array[:,:,5],cmap='jet',norm=norm,shading='gouraud',vmin=0.,vmax=0.6)
Run Code Online (Sandbox Code Playgroud)