pcolormesh:'NoneType' 对象没有属性 'spines'

问题描述

我想使用 pcolormesh 构建一个显示 6 个栅格 (.tif) 的图形。我使用光栅从 UTM 坐标计算纬度坐标

with rio.open(T_files[0]) as r:
T0 = r.transform
p1 = Proj(r.crs)
T = r.read()
Turbidity = r.read(1)

cols,rows = np.meshgrid(np.arange(T.shape[2]),np.arange(T.shape[1]))

T1 = T0*Affine.translation(0.5,0.5)
rc2en = lambda r,c: (c,r)*T1

eastings,northings = np.vectorize(rc2en,otypes=[np.float,np.float])(rows,cols)

p2 = Proj(proj='latlong',datum='wgs84')
longs_CD,lats_CD = transform(p1,p2,eastings,northings)

这段代码用于每个光栅,因为由于卫星轨道不同,它们的尺寸都略有不同。之后我尝试使用

绘制该图
fig1 = plt.figure(figsize=(30,10),dpi=150)
fig1.patch.set_alpha(1)
#fig.add_subplot(ROW,COLUMN,POSITION)
ax0 = plt.subplot(131,projection=ccrs.Mercator())
ax1 = plt.subplot(132,projection=ccrs.Mercator())
ax2 = plt.subplot(133,projection=ccrs.Mercator())
ax3 = plt.subplot(231,projection=ccrs.Mercator())
ax4 = plt.subplot(232,projection=ccrs.Mercator())
ax5 = plt.subplot(233,projection=ccrs.Mercator())

f0 = ax0.pcolormesh(longs_CD,lats_CD,np.ma.masked_invalid(TCD),shading='auto',transform = ccrs.PlateCarree(),vmin=1,vmax=200,cmap=plt.cm.rainbow,norm=colors.Lognorm(vmin=1,vmax=200))
    
f1 = ax1.pcolormesh(longs_ND,lats_ND,np.ma.masked_invalid(TND),vmax=200))

f2 = ax2.pcolormesh(longs_ED,lats_ED,np.ma.masked_invalid(TED),vmax=200))

f3 = ax3.pcolormesh(longs_CK,lats_CK,np.ma.masked_invalid(TCK),vmax=200))

f4 = ax4.pcolormesh(longs_NK,lats_NK,np.ma.masked_invalid(TNK),vmax=200))

f5 = ax5.pcolormesh(longs_EK,lats_EK,np.ma.masked_invalid(TEK),vmax=200))

g1 = ax0.gridlines(draw_labels = True)
g1.ylabels_right = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20,'color': 'k'}
g1.ylabel_style = {'size': 20,'color': 'k'}

g1 = ax1.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20,'color': 'k'}

g1 = ax2.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20,'color': 'k'}

g1 = ax3.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20,'color': 'k'}

g1 = ax4.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20,'color': 'k'}

g1 = ax5.gridlines(draw_labels = True)
g1.ylabels_left = None
g1.xlabels_bottom = None
g1.xlabel_style = {'size': 20,'color': 'k'}

cb1 = fig1.colorbar(f1,orientation="horizontal",fraction=0.05,pad=0.07)
cb1.set_label('mean Turbidity [FNU]',fontsize=18)
cb1.tick_params(labelsize=20)
 
plt.show()
plt.clf()

类似的代码片段对我来说通常运行良好,但这次出现错误:AttributeError: 'nonetype' object has no attribute 'spines'。我很确定这很容易解决,但我无法弄清楚。这是完整的错误信息

AttributeError                            Traceback (most recent call last)
<ipython-input-12-ced9235ef932> in <module>
     11 f0 = ax0.pcolormesh(longs_CD,12                     shading='auto',---> 13                     vmin=1,vmax=200))
     14 
     15 f1 = ax1.pcolormesh(longs_ND,~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in wrapper(self,*args,**kwargs)
    308 
    309         kwargs['transform'] = transform
--> 310         return func(self,**kwargs)
    311     return wrapper
    312 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in pcolormesh(self,**kwargs)
   1559 
   1560         """
-> 1561         result = self._pcolormesh_patched(*args,**kwargs)
   1562         self.autoscale_view()
   1563         return result

~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in _pcolormesh_patched(self,**kwargs)
   1657         self.update_datalim(corners)
   1658         self.add_collection(collection)
-> 1659         self.autoscale_view()
   1660 
   1661         ########################

~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in autoscale_view(self,tight,scalex,scaley)
    853     def autoscale_view(self,tight=None,scalex=True,scaley=True):
    854         matplotlib.axes.Axes.autoscale_view(self,tight=tight,--> 855                                             scalex=scalex,scaley=scaley)
    856         # Limit the resulting bounds to valid area.
    857         if scalex and self._autoscaleXon:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in autoscale_view(self,scaley)
   2579         handle_single_axis(
   2580             scalex,self._autoscaleXon,self._shared_x_axes,'intervalx',-> 2581             'minposx',self.xaxis,self._xmargin,x_stickies,self.set_xbound)
   2582         handle_single_axis(
   2583             scaley,self._autoscaleYon,self._shared_y_axes,'intervaly',~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in handle_single_axis(scale,autoscaleon,shared_axes,interval,minpos,axis,margin,stickies,set_bound)
   2574             if not self._tight:
   2575                 x0,x1 = locator.view_limits(x0,x1)
-> 2576             set_bound(x0,x1)
   2577             # End of deFinition of internal function 'handle_single_axis'.
   2578 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in set_xbound(self,lower,upper)
   3170         self.set_xlim(sorted((lower,upper),3171                              reverse=bool(self.xaxis_inverted())),-> 3172                       auto=None)
   3173 
   3174     def get_xlim(self):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_base.py in set_xlim(self,left,right,emit,auto,xmin,xmax)
   3338 
   3339         if emit:
-> 3340             self.callbacks.process('xlim_changed',self)
   3341             # Call all of the other x-axes that are shared with this one
   3342             for other in self._shared_x_axes.get_siblings(self):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in process(self,s,**kwargs)
    227                 except Exception as exc:
    228                     if self.exception_handler is not None:
--> 229                         self.exception_handler(exc)
    230                     else:
    231                         raise

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in _exception_printer(exc)
     79 def _exception_printer(exc):
     80     if _get_running_interactive_framework() in ["headless",None]:
---> 81         raise exc
     82     else:
     83         traceback.print_exc()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in process(self,**kwargs)
    222             if func is not None:
    223                 try:
--> 224                     func(*args,**kwargs)
    225                 # this does not capture KeyboardInterrupt,SystemExit,226                 # and GeneratorExit

~\AppData\Local\Continuum\anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in _trigger_patch_reclip(event)
   2060     axes = event.axes
   2061     # trigger the outline and background patches to be re-clipped
-> 2062     axes.spines['geo'].stale = True
   2063     axes.patch.stale = True

AttributeError: 'nonetype' object has no attribute 'spines'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...