彩条/绘图问题? “posx和posy应该是有限的值”

时间:2016-10-18 19:55:51

标签: python arrays matplotlib jupyter-notebook colorbar

问题

所以我有一个带有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)

enter image description here

现在,当我使用完全相同的代码但是为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

我不知道为什么它这样做,因为我的代码对于数组的每个其他部分都很好,并且它们都使用相同的meshgrid。数组中没有NaN。如果我在#Colorbar#Titles & labels

之间注释掉所有代码,也会产生结果

enter image description here

更新:当我添加彩条代码但将PowerNorm更改为1.0norm = 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 "

1 个答案:

答案 0 :(得分:4)

所以我发现指定vmax&amp; vmin解决了这个问题。我不明白为什么,但是一旦我这样做,我的情节就会用颜色条正确显示出来。

trend = m.pcolormesh(x,y,array[:,:,5],cmap='jet',norm=norm,shading='gouraud',vmin=0.,vmax=0.6)

enter image description here