valueerror - 在 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, 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.
解决方案
这是 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 应该有新版本来解决这个问题。
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射