Skip to content

Failed to init RangeSlider with valinit attribute #19960

@EddieHyun

Description

@EddieHyun

Bug report

Bug summary

Creating RangeSlider with valinit attribute causes an error AttributeError: 'RangeSlider' object has no attribute 'val'

# matplotlib/widgets.py:611
        if valinit is None:
            # Place at the 25th and 75th percentiles
            extent = valmax - valmin
            valinit = np.array(
                [valmin + extent * 0.25, valmin + extent * 0.75]
            )
        else:
            valinit = self._value_in_bounds(valinit)
        self.val = valinit

self.val is used in the self._value_in_bounds(valinit) call before it is initialized.
Last line, self.val = valinit should come first before validating the init bounds.

Code for reproduction

from matplotlib import pyplot as plt
from matplotlib.widgets import RangeSlider

x = plt.axes([0.9, 0.2, 0.03, 0.65])
rslider = RangeSlider(x, 'range', valinit=(0.2, 0.5), valmin=0.0, valmax=1.0)

Actual outcome

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...\venv\lib\site-packages\matplotlib\widgets.py", line 618, in __init__
    valinit = self._value_in_bounds(valinit)
  File "...\venv\lib\site-packages\matplotlib\widgets.py", line 688, in _value_in_bounds
    return (self._min_in_bounds(val[0]), self._max_in_bounds(val[1]))
  File "...\venv\lib\site-packages\matplotlib\widgets.py", line 672, in _min_in_bounds
    if min > self.val[1]:
AttributeError: 'RangeSlider' object has no attribute 'val'

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.1
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.8.6
  • matplotlib is installed from pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions