Skip to content

[Bug]: set_val of rangeslider sets incorrect value #25338

Closed
@pinna9001

Description

@pinna9001

Bug summary

The set_val() method of a range slider doesn't set the value correctly with values close to the minimal and maximal values of the range slider. With values in the middle, everything works fine.

Code for reproduction

import matplotlib.pyplot as pyplot
import matplotlib.widgets as widgets

ax = pyplot.axes([0.15, 0.01, 0.7, 0.1])
ax2 = pyplot.axes([0.15, 0.21, 0.7, 0.1])
ax3 = pyplot.axes([0.15, 0.41, 0.7, 0.1])
ax4 = pyplot.axes([0.15, 0.61, 0.7, 0.1])

# correct behaviour with set_val using values in the middle between val min and val max
correct2 = widgets.RangeSlider(ax, "correct2", valmin=1.0, valmax=10.0)
correct2.set_val((4, 6))
print(correct2.val)

# correct with val init 
correct = widgets.RangeSlider(ax2, "correct", valinit=(1.0, 2.0), valmin=1.0, valmax=10.0)
print(correct.val)

# wrong with set_val having values close to the value max
wrong2 = widgets.RangeSlider(ax3, "wrong2", valmin=1.0, valmax=10.0)
wrong2.set_val((9, 10))
print(wrong2.val)

# wrong with set_val having values close to the value min
wrong = widgets.RangeSlider(ax4, "wrong", valmin=1.0, valmax=10.0)
wrong.set_val((1, 2))
print(wrong.val)

pyplot.show()

Actual outcome

grafik
The values for the rangesliders "wrong" and "wrong2" are not set correctly
Expected values are: (1, 2) and (9, 10)
Actual values are (1, 3.25) and (7.75, 10)

Expected outcome

The result of using set_val should be the same, as when using the valinit parameter of the constructor.

Additional information

The problem also occurred on Manjaro with:

  • Python version: 3.10.9
  • Matplotlib version: 3.6.2
  • Matplotlib backend: QtAgg
  • Installation of matplotlib via Linux package manager

Operating system

Windows 10

Matplotlib Version

3.6.2

Matplotlib Backend

TkAgg

Python version

3.11.0

Jupyter version

No response

Installation

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