Skip to content

[Bug]: RangeSlider.set_val does not move the slider (only poly and value) #22706

Closed

Description

@vpicouet

Bug summary

When I call RangeSlider.set_val([0.0,0.1])
It changes only the blue poly object and the range value on the right side of the slider not the dot:
Capture d’écran, le 2022-03-25 à 18 00 59

Code for reproduction

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

# generate a fake image
np.random.seed(19680801)
N = 128
img = np.random.randn(N, N)

fig, axs = plt.subplots(1, 2, figsize=(10, 5))
fig.subplots_adjust(bottom=0.25)

im = axs[0].imshow(img)
axs[1].hist(img.flatten(), bins='auto')
axs[1].set_title('Histogram of pixel intensities')

# Create the RangeSlider
slider_ax = fig.add_axes([0.20, 0.1, 0.60, 0.03])
slider = RangeSlider(slider_ax, "Threshold", img.min(), img.max(),valinit=[0.0,1])

# Create the Vertical lines on the histogram
lower_limit_line = axs[1].axvline(slider.val[0], color='k')
upper_limit_line = axs[1].axvline(slider.val[1], color='k')


def update(val):
    # The val passed to a callback by the RangeSlider will
    # be a tuple of (min, max)

    # Update the image's colormap
    im.norm.vmin = val[0]
    im.norm.vmax = val[1]

    # Update the position of the vertical lines
    lower_limit_line.set_xdata([val[0], val[0]])
    upper_limit_line.set_xdata([val[1], val[1]])

    # Redraw the figure to ensure it updates
    fig.canvas.draw_idle()

slider.set_val([0.0,5])
slider.on_changed(update)
plt.show()

Actual outcome

I does not move the slider
Capture d’écran, le 2022-03-25 à 18 04 26

Expected outcome

It should move the slider

Additional information

No response

Operating system

OSX

Matplotlib Version

3.5.1

Matplotlib Backend

No response

Python version

3.8

Jupyter version

No response

Installation

pip

Activity

  1. ianhi commented on Mar 25, 2022

    @ianhi
    Contributor

    Ahh actually there is yet another bug with the handles that I discovered looking at the code for this. For a vertical rangeslider the handle positions go bananas:

    import matplotlib.pyplot as plt
    from matplotlib.widgets import RangeSlider
    
    fig, ax = plt.subplots()
    rs = RangeSlider(ax, "blarg", 0, 1, orientation="vertical")
    plt.show()

    broken-range-slider

    This vertical bug, and the original bug both stem from the fact that the only updating of the slider handle position happens here:

    if self._active_handle:
    self._active_handle.set_xdata([val])
    when really it should be happening in set_val next to where we update the poly

  2. nik1097 commented on Mar 26, 2022

    @nik1097

    I will try recreating and fixing this issue to if that's okay @vpicouet @ianhi

  3. vpicouet commented on Mar 26, 2022

    @vpicouet
    Author

    Yes!

  4. andrew-fennell commented on Mar 28, 2022

    @andrew-fennell
    Contributor

    The PR #22711 will resolve the issues with both horizontal and vertical RangeSlider handle positions when using set_val.

  5. added this to the v3.5.2 milestone on Mar 30, 2022
  6. QuLogic commented on Mar 30, 2022

    @QuLogic
    Member

    Fixed by #22711.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions