如何修复错误 运行 本地 Otsu 阈值示例?

How to fix error running local Otsu threshold example?

我对 Python 很陌生。我正在尝试 运行 这个脚本:

https://scikit-image.org/docs/0.12.x/auto_examples/segmentation/plot_local_otsu.html

但是,我收到了这个错误:

Traceback (most recent call last):
File "/Users/janine/Downloads/plot_local_otsu.py", line 37, in <module>
  fig, ax = plt.subplots(2, 2, figsize=(8, 5), sharex=True, sharey=True,
File "/usr/local/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 471, in wrapper
  return func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/matplotlib/pyplot.py", line 1440, in subplots
  axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
File "/usr/local/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 471, in wrapper
  return func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/matplotlib/figure.py", line 908, in subplots
  axs = gs.subplots(sharex=sharex, sharey=sharey, squeeze=squeeze,
File "/usr/local/lib/python3.9/site-packages/matplotlib/gridspec.py", line 307, in subplots
  axarr[row, col] = figure.add_subplot(
File "/usr/local/lib/python3.9/site-packages/matplotlib/figure.py", line 781, in add_subplot
  ax = subplot_class_factory(projection_class)(self, *args, **pkw)
File "/usr/local/lib/python3.9/site-packages/matplotlib/axes/_subplots.py", line 36, in __init__
  self._axes_class.__init__(self, fig, [0, 0, 1, 1], **kwargs)
File "/usr/local/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 471, in wrapper
  return func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 648, in __init__
  self.update(kwargs)
File "/usr/local/lib/python3.9/site-packages/matplotlib/artist.py", line 1064, in update
  ret.append(func(v))
File "/usr/local/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 1531, in set_adjustable
  _api.check_in_list(["box", "datalim"], adjustable=adjustable)
File "/usr/local/lib/python3.9/site-packages/matplotlib/_api/__init__.py", line 126, in check_in_list
  raise ValueError(
    ValueError: 'box-forced' is not a valid value for adjustable; supported values are 'box', 'datalim'

我完全按照此处的建议安装了 scikit-image

https://scikit-image.org/docs/stable/install.html.

我在 macOS Mojave 上。

正如您在 link 中看到的,该示例来自 skimage 的过时 0.12.x 版本,而 0.18.0 是当前的稳定版本。而且,正如错误消息所示,错误来自这一行:

fig, ax = plt.subplots(2, 2, figsize=(8, 5), sharex=True, sharey=True,
                       subplot_kw={'adjustable': 'box-forced'})

显然,adjustable 成员的处理方式在 matplotlib.pyplot 多年来发生了变化。例如,通过简单地完全删除 subplot_kw 参数,代码运行得非常好:

fig, ax = plt.subplots(2, 2, figsize=(8, 5), sharex=True, sharey=True)

事实上,这也是 skimage 文档中的 updated example 的样子(那里的第二个例子)。注意:您必须自己添加两个 import 语句,因为那里给定的代码不完整。