在Metpy上绘制组合图时ValueError的原因

运行示例代码时出现ValueError

我从以下网站获得了示例代码:https://unidata.github.io/MetPy/latest/examples/plots/Combined_plotting.html

我想知道为什么示例程序( Combined_plotting.py Combined_plotting.ipynb )不能在我的计算机上工作。

原始代码在下面

#  Copyright (c) 2019 MetPy Developers.
#  Distributed under the terms of the BSD 3-Clause License.
#  SPDX-License-Identifier: BSD-3-Clause
"""
Combined Plotting
=================

Demonstrate the use of MetPy's simplified plotting interface combining multiple plots.

Also shows how to control the maps that are plotted. Plots sample NARR data.
"""

import xarray as xr

from metpy.cbook import get_test_data
from metpy.plots import ContourPlot,ImagePlot,MapPanel,PanelContainer
from metpy.units import units

# Use sample NARR data for plotting
narr = xr.open_dataset(get_test_data('narr_example.nc',as_file_obj=False))

###########################
# Create a contour plot of temperature
contour = ContourPlot()
contour.data = narr
contour.field = 'Temperature'
contour.level = 850 * units.hPa
contour.linecolor = 'red'
contour.contours = 15

###########################
# Create an image plot of Geopotential height
img = ImagePlot()
img.data = narr
img.field = 'Geopotential_height'
img.level = 850 * units.hPa


###########################
# Plot the data on a map
panel = MapPanel()
panel.area = 'us'
panel.layers = ['coastline','borders','states','rivers','ocean','land']
panel.title = 'NARR Example'
panel.plots = [contour,img]

pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()

错误信息在下面

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-3cbf22151612> in <module>
      8 pc.size = (10,8)
      9 pc.panels = [panel]
---> 10 pc.show()

/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in show(self)
    568     def show(self):
    569         """Show the constructed graphic on the screen."""
--> 570         self.draw()
    571         plt.show()
    572 

/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in draw(self)
    555         for panel in self.panels:
    556             with panel.hold_trait_notifications():
--> 557                 panel.draw()
    558 
    559     def save(self,*args,**kwargs):

/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in draw(self)
    734             for p in self.plots:
    735                 with p.hold_trait_notifications():
--> 736                     p.draw()
    737 
    738             # Add all of the maps

/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in draw(self)
    945         if self._need_redraw:
    946             if getattr(self,'handle',None) is None:
--> 947                 self._build()
    948             if getattr(self,'colorbar',None) is not None:
    949                 self.parent.ax.figure.colorbar(

/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in _build(self)
   1083     def _build(self):
   1084         """Build the plot by calling any plotting methods as necessary."""
-> 1085         x,y,imdata = self.plotdata
   1086         self.handle = self.parent.ax.contour(x,imdata,self.contours,1087                                              colors=self.linecolor,linewidths=self.linewidth,/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in plotdata(self)
    930 
    931         """
--> 932         x = self.griddata.metpy.x
    933         y = self.griddata.metpy.y
    934 

/opt/anaconda3/lib/python3.7/site-packages/metpy/plots/declarative.py in griddata(self)
    905 
    906             if self.field:
--> 907                 data = self.data.metpy.parse_cf(self.field)
    908 
    909             elif not hasattr(self.data.metpy,'x'):

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in parse_cf(self,varname,coordinates)
    518                 var.coords['crs'] = CFProjection(proj_var.attrs)
    519 
--> 520         self._fixup_coords(var)
    521 
    522         # Trying to guess whether we should be adding a crs to this variable's coordinates

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in _fixup_coords(self,var)
    547                     and not check_axis(data_array,'longitude','latitude')):
    548                 try:
--> 549                     var.coords[coord_name].metpy.convert_units('meters')
    550                 except DimensionalityError:  # Radians!
    551                     if 'crs' in var.coords:

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in convert_units(self,units)
    143     def convert_units(self,units):
    144         """Convert the data values to different units in-place."""
--> 145         self.unit_array = self.unit_array.to(units)
    146 
    147     @property

/opt/anaconda3/lib/python3.7/site-packages/metpy/xarray.py in unit_array(self,values)
    138     def unit_array(self,values):
    139         """Set data values from a `pint.Quantity`."""
--> 140         self._data_array.values = values.magnitude
    141         self._units = self._data_array.attrs['units'] = str(values.units)
    142 

/opt/anaconda3/lib/python3.7/site-packages/xarray/core/common.py in __setattr__(self,name,value)
    260         """
    261         try:
--> 262             object.__setattr__(self,value)
    263         except AttributeError as e:
    264             # Don't accidentally shadow custom AttributeErrors,e.g.

/opt/anaconda3/lib/python3.7/site-packages/xarray/core/dataarray.py in values(self,value)
    560     @values.setter
    561     def values(self,value: Any) -> None:
--> 562         self.variable.values = value
    563 
    564     @property

/opt/anaconda3/lib/python3.7/site-packages/xarray/core/variable.py in values(self,values)
   2113     def values(self,values):
   2114         raise ValueError(
-> 2115             f"Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
   2116             f"Please use DataArray.assign_coords,Dataset.assign_coords or Dataset.assign as appropriate."
   2117         )

ValueError: Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable 'y'. Please use DataArray.assign_coords,Dataset.assign_coords or Dataset.assign as appropriate.
tskpzx 回答:在Metpy上绘制组合图时ValueError的原因

这是MetPy 0.12(和1.0.0rc1)与xarray 0.15.1之间的不兼容。目前,解决方法是安装xarray 0.15.0(pip install xarray==0.15.0conda intall xarray=0.15.0)。

MetPy应该具有新版本来解决此问题。

本文链接:https://www.f2er.com/2515251.html

大家都在问