问题
最近用到了py-eddy-tracker, 参照官方文档进行漩涡区域填充时发现调用py_eddy_tracker.observations.observation.EddiesObservations.filled
报错:
'EddiesObservations' object has no attribute 'filled'
尝试
filled函数的官方文档
查看官网提供的源码发现是有filled函数的,但是自己本地的库中没有.
检查版本发现本地版本为3.2.0, 官方文档版本为3.5.0
> pip show pyEddyTracker
Name: pyEddyTracker
Version: 3.2.0
...
而且pip无法安装3.5.0
> pip install pyEddyTracker==3.5.0
ERROR: Could not find a version that satisfies the requirement pyEddyTracker==3.5.0 (from versions: 3.0.0, 3.1.0, 3.2.0)
ERROR: No matching distribution found for pyEddyTracker==3.5.0
尝试使用文档提供的办法手动安装最新版本:
最后提示版本过低, 我的python3.6无法安装最新版的py-eddy-tracker
RuntimeError: Cannot install on Python version 3.6.4; only versions >=3.7,<3.10 are supported
解决方法
升级python
升级python版本是不可能的, 这辈子都不可能的.
手动修改observation.py源码
# 32行+
from matplotlib.cm import get_cmap
from matplotlib.collections import PolyCollection
from matplotlib.colors import Normalize
# 1259行+
def parse_varname(self, name):
return self[name] if isinstance(name, str) else name
def format_label(self, label):
t0, t1 = self.period
return label.format(
t0=t0,
t1=t1,
nb_obs=len(self),
)
def filled(
self,
ax,
varname=None,
ref=None,
intern=False,
cmap="magma_r",
lut=10,
vmin=None,
vmax=None,
factor=1,
**kwargs,
):
"""
:param matplotlib.axes.Axes ax: matplotlib axe used to draw
:param str,array,None varname: variable used to fill the contours, or an array of same size than obs
:param float,None ref: if defined, all coordinates are wrapped with ref as western boundary
:param bool intern: if True draw speed contours instead of effective contours
:param str cmap: matplotlib colormap name
:param int,None lut: Number of colors in the colormap
:param float,None vmin: Min value of the colorbar
:param float,None vmax: Max value of the colorbar
:param float factor: multiply value by
:return: Collection drawed
:rtype: matplotlib.collections.PolyCollection
.. minigallery:: py_eddy_tracker.EddiesObservations.filled
"""
x_name, y_name = self.intern(intern)
x, y = self[x_name], self[y_name]
if ref is not None:
# TODO : maybe buggy with global display
shape_out = x.shape
x, y = wrap_longitude(x.reshape(-1), y.reshape(-1), ref)
x, y = x.reshape(shape_out), y.reshape(shape_out)
verts = list()
for x_, y_ in zip(x, y):
verts.append(create_vertice(x_, y_))
if "facecolors" not in kwargs:
kwargs = kwargs.copy()
cmap = get_cmap(cmap, lut)
v = self.parse_varname(varname) * factor
if vmin is None:
vmin = v.min()
if vmax is None:
vmax = v.max()
v = (v - vmin) / (vmax - vmin)
colors = [cmap(v_) for v_ in v]
kwargs["facecolors"] = colors
if "label" in kwargs:
kwargs["label"] = self.format_label(kwargs["label"])
c = PolyCollection(verts, **kwargs)
ax.add_collection(c)
c.cmap = cmap
c.norm = Normalize(vmin=vmin, vmax=vmax)
return c
结果
成功调用
fig = plt.figure(figsize=(3, 2))
ax = fig.add_axes([.03, .03, .94, .94])
ax.set_ylim(4, 30)
ax.set_xlim(105.5, 150)
plt.axis('off')
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
ax.set_aspect('equal')
a.filled(ax, 'amplitude', vmin=0, vmax=.5, color='b') # 填充
c.filled(ax, 'amplitude', vmin=0, vmax=.5, color='r')
plt.show()