python - 彩条/绘图问题? "posx and posy should be finite values"

标签 python arrays matplotlib jupyter-notebook colorbar

问题

所以我有一个 6 层 (array.size = (192,288,6)) 的经纬度数组,其中包含一堆数据,其值范围接近 0 到大约 0.65。当我绘制来自 6 层([:,:,0][:,:,1] 等的每一层的数据时.),除了 [:,:,4] 之外,我没有任何问题并得到了一张漂亮的 map 。出于某种原因,当我尝试绘制此 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

我不知道为什么它会这样做,因为我的数组其他部分的代码绘制得很好,而且它们都使用相同的网格。数组中没有 NaN。如果我注释掉 #Colorbar#Titles & labels

之间的所有代码,这也是结果

enter image description here

更新:当我包含颜色条代码但将 PowerNorm 更改为 1.0 时,问题也消失了(norm = colors.PowerNorm(1.0,vmax=0.65 ))。当包含颜色栏时,1.0 以外的任何内容都会产生错误。

问题

是什么导致了 posxposy 错误消息,我怎样才能摆脱它,以便我可以使用包含颜色条的绘图?

更新

当我从头开始运行内核时,再次使用相同的代码(除了我将 0.6 更改为 0.65 之外),我在 中收到以下警告code>array[:,:,4] block 。我不确定它们是否相关,但我会把它们包括在内以防万一。

/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 "

最佳答案

所以我发现指定 vmax & vmin 可以解决问题。我不知道为什么,但一旦我这样做了,我的情节就用颜色条正确地显示出来了。

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

enter image description here

关于python - 彩条/绘图问题? "posx and posy should be finite values",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116968/

相关文章:

java - 如何对对象数组列表中的整数进行排序?

python - 在 matplotlib 中映射六边形网格

python - x 刻度和标签的自定义定位

python - 为什么 'matplotlib' 无法找到指定字体的某些字体粗细?

python - wxPython 选中和取消选中复选框

c - while 循环中字符串数组末尾出现段错误

python - 如何根据一列的值将numpy数组拆分为子数组

python - 使用 "genfromtxt"提取列

python从div block 中获取数据

python - 在没有imp.load_dynamic的情况下将DLL导入Python 3