首页 > 解决方案 > How to fix error running local Otsu threshold example?

问题描述

I am very new to Python. I am trying to run this script:

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

But, I'm getting this error:

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'

I have installed scikit-image exactly as recommended here:

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

I am on macOS Mojave.

标签: pythonmatplotlibscikit-image

解决方案


As you can see from the link, the example is from the outdated 0.12.x version(s) of skimage, whereas 0.18.0 is current stable version. And, as the error message indicates, the error comes from this line:

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

Obviously, the handling of the adjustable member has changed in matplotlib.pyplot over the years. By simply removing the subplot_kw parameter at all, for example, the code runs perfectly fine:

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

In fact, that's also, what the updated example from the skimage documentation looks like (second example there). Notice: You'll have to add two import statements on your own, since the given code there in incomplete.


推荐阅读