区间数据的 Cartopy 绘图问题
Cartopy plotting issue for interval data
我正在尝试以离散间隔在 Cartopy 网格上绘制数据。数据范围从 0 到 1,间隔为 0.05。在某些(看似随机的)情况下 Python 会抛出一条错误消息:
IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
在此之后弹出另一个 OSError(问题末尾给出回溯)。对于下面的简短示例,该问题在大约 70% 的时间内重现:
V=np.arange(-1,1.05,0.05)
array_fill = np.random.random((71,361))*20//1/20
plt.figure()
ax = plt.axes(projection = ccrs.PlateCarree())
proj = ccrs.PlateCarree()
lon = np.arange(0,361)
lat = np.arange(20,91)
ax.coastlines(resolution='110m')
ax.gridlines()
ax.contourf(lon,lat,array_fill, V, cmap = cm.jet)
随机性让我觉得错误只发生在某些配置上(比如,1 与 -1 在两边并置)。
此错误的一个不寻常方面是,当不包括日期线时(例如,仅绘制从 0 到 170E 的图),它会变得不那么频繁。即使在短范围内(例如,170E 到 170W)包含日期线,也会再次出现错误,频率约为 70%。
我的问题是:示例中出了什么问题?或者这可能是 Cartopy 的内部错误?
这是初始错误消息后的完整追溯报告:
Traceback (most recent call last):
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5.py", line 519, in _draw_idle
self.draw()
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\figure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\mpl\geoaxes.py", line 385, in draw
inframe=inframe)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 911, in draw
Collection.draw(self, renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 266, in draw
transform, transOffset, offsets, paths = self._prepare_points()
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 244, in _prepare_points
for path in paths]
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 244, in <listcomp>
for path in paths]
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\transforms.py", line 2499, in transform_path_non_affine
return self._a.transform_path_non_affine(path)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\mpl\geoaxes.py", line 193, in transform_path_non_affine
geom, self.source_projection)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\crs.py", line 181, in project_geometry
return getattr(self, method_name)(geometry, src_crs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\crs.py", line 336, in _project_polygon
return self._rings_to_multi_polygon(rings, is_ccw)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\crs.py", line 526, in _rings_to_multi_polygon
if ring.is_ccw != is_ccw:
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\geometry\polygon.py", line 86, in is_ccw
return bool(self.impl['is_ccw'](self))
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\algorithms\cga.py", line 14, in is_ccw_op
return signed_area(ring) >= 0.0
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\algorithms\cga.py", line 6, in signed_area
xs, ys = ring.coords.xy
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\geometry\base.py", line 322, in _get_coords
if self.is_empty:
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\geometry\base.py", line 643, in is_empty
return (self._geom is None) or bool(self.impl['is_empty'](self))
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\predicates.py", line 25, in __call__
return self.fn(this._geom)
OSError: exception: access violation reading 0x0000000000000000
Cartopy 版本为 0.16。
谢谢!
您的等高线数据必须对处理有效。这是工作代码。
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.cm as cm
V = np.arange(-1, 1.05, 0.05)
# The data must be valid for contouring
# with (*20//1/20), the data is not fine-grained enough
array_fill = np.random.random((71, 361))*2000//1/2000
plt.figure(figsize=[12,8])
ax = plt.axes(projection = ccrs.PlateCarree())
# proj = ccrs.PlateCarree()
lon = np.arange(0, 361)
lat = np.arange(20, 91)
ax.coastlines(resolution='110m', color='blue', linewidth=2)
ax.gridlines()
# (lon, lat) can't be used directly,
# meshgrid must be created from them
xs, ys = np.meshgrid(lon, lat)
ax.contourf(xs, ys, array_fill, V, cmap = cm.jet)
plt.show()
生成的图像:
我正在尝试以离散间隔在 Cartopy 网格上绘制数据。数据范围从 0 到 1,间隔为 0.05。在某些(看似随机的)情况下 Python 会抛出一条错误消息:
IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
在此之后弹出另一个 OSError(问题末尾给出回溯)。对于下面的简短示例,该问题在大约 70% 的时间内重现:
V=np.arange(-1,1.05,0.05)
array_fill = np.random.random((71,361))*20//1/20
plt.figure()
ax = plt.axes(projection = ccrs.PlateCarree())
proj = ccrs.PlateCarree()
lon = np.arange(0,361)
lat = np.arange(20,91)
ax.coastlines(resolution='110m')
ax.gridlines()
ax.contourf(lon,lat,array_fill, V, cmap = cm.jet)
随机性让我觉得错误只发生在某些配置上(比如,1 与 -1 在两边并置)。
此错误的一个不寻常方面是,当不包括日期线时(例如,仅绘制从 0 到 170E 的图),它会变得不那么频繁。即使在短范围内(例如,170E 到 170W)包含日期线,也会再次出现错误,频率约为 70%。
我的问题是:示例中出了什么问题?或者这可能是 Cartopy 的内部错误?
这是初始错误消息后的完整追溯报告:
Traceback (most recent call last):
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5.py", line 519, in _draw_idle
self.draw()
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\figure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\mpl\geoaxes.py", line 385, in draw
inframe=inframe)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 911, in draw
Collection.draw(self, renderer)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 266, in draw
transform, transOffset, offsets, paths = self._prepare_points()
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 244, in _prepare_points
for path in paths]
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\collections.py", line 244, in <listcomp>
for path in paths]
File "C:\Program Files (x86)\Anaconda\lib\site-packages\matplotlib\transforms.py", line 2499, in transform_path_non_affine
return self._a.transform_path_non_affine(path)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\mpl\geoaxes.py", line 193, in transform_path_non_affine
geom, self.source_projection)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\crs.py", line 181, in project_geometry
return getattr(self, method_name)(geometry, src_crs)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\crs.py", line 336, in _project_polygon
return self._rings_to_multi_polygon(rings, is_ccw)
File "C:\Program Files (x86)\Anaconda\lib\site-packages\cartopy\crs.py", line 526, in _rings_to_multi_polygon
if ring.is_ccw != is_ccw:
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\geometry\polygon.py", line 86, in is_ccw
return bool(self.impl['is_ccw'](self))
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\algorithms\cga.py", line 14, in is_ccw_op
return signed_area(ring) >= 0.0
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\algorithms\cga.py", line 6, in signed_area
xs, ys = ring.coords.xy
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\geometry\base.py", line 322, in _get_coords
if self.is_empty:
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\geometry\base.py", line 643, in is_empty
return (self._geom is None) or bool(self.impl['is_empty'](self))
File "C:\Program Files (x86)\Anaconda\lib\site-packages\shapely\predicates.py", line 25, in __call__
return self.fn(this._geom)
OSError: exception: access violation reading 0x0000000000000000
Cartopy 版本为 0.16。
谢谢!
您的等高线数据必须对处理有效。这是工作代码。
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.cm as cm
V = np.arange(-1, 1.05, 0.05)
# The data must be valid for contouring
# with (*20//1/20), the data is not fine-grained enough
array_fill = np.random.random((71, 361))*2000//1/2000
plt.figure(figsize=[12,8])
ax = plt.axes(projection = ccrs.PlateCarree())
# proj = ccrs.PlateCarree()
lon = np.arange(0, 361)
lat = np.arange(20, 91)
ax.coastlines(resolution='110m', color='blue', linewidth=2)
ax.gridlines()
# (lon, lat) can't be used directly,
# meshgrid must be created from them
xs, ys = np.meshgrid(lon, lat)
ax.contourf(xs, ys, array_fill, V, cmap = cm.jet)
plt.show()
生成的图像: