Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorbar/plotting issue? "posx and posy should be finite values"

The problem

So I have a lat-lon array with 6 layers (array.size = (192,288,6)) containing a bunch of data ranging in values from nearly 0 to about 0.65. When I plot data from every one of the 6 layers ([:,:,0], [:,:,1], etc.), I have no problems and get a nice map, except for [:,:,4]. For some reason, when I try to plot this 2D array, I get an error message I don't understand, and it only comes up when I try to include a colorbar. If I nix the colorbar there's no error, but I need that colorbar...

The code

Here's the code I use for a different part of the array, along with the resulting plot. Let's go with [:,:,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

Now when I use the EXACT same code but plot for array[:,:,4] instead of array[:,:,5], I get this error.

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

I have no idea why it's doing this as my code for every other part of the array plots just fine, and they all use the same meshgrid. There are no NaN's in the array. Also here's the result if I comment out all the code between #Colorbar and #Titles & labels

enter image description here

UPDATE: The problem also disappears when I include the colorbar code but changed the PowerNorm to 1.0 (norm = colors.PowerNorm(1.0,vmax=0.65)). Anything other than 1.0 generates the error when the colorbar is included.

The question

What could be causing the posx & posy error message, and how can I get rid of it so I can make this plot with the colorbar included?

UPDATE

When I run the kernel from scratch, again with the same code (except that I changed the 0.6 bound to 0.65), I get the following warnings in the array[:,:,4] block. I'm not sure if they're related, but I'll include them just in case.

/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 "
like image 800
ChristineB Avatar asked Oct 18 '16 19:10

ChristineB


1 Answers

So I found out that specifying vmax & vmin solves the problem. I have no idea why, but once I did, my plot turned out correctly with the colorbar.

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

enter image description here

like image 184
ChristineB Avatar answered Sep 21 '22 21:09

ChristineB