Причина ошибки ValueError при построении комбинированного графика на Metpy - PullRequest
0 голосов
/ 04 апреля 2020

Ошибка 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, y, 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, name, 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.

1 Ответ

0 голосов
/ 05 апреля 2020

Это несовместимость между MetPy 0.12 (и 1.0.0rc1) и xarray 0.15.1. В настоящее время обходной путь должен установить xarray 0.15.0 (pip install xarray==0.15.0 или conda intall xarray=0.15.0).

У MetPy должны быть новые выпуски для решения этой проблемы.

...