Skip to content

Commit 8033f88

Browse files
committed
create RangeSlider widget
1 parent 74d6145 commit 8033f88

File tree

4 files changed

+420
-58
lines changed

4 files changed

+420
-58
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
New RangeSlider widget
2+
----------------------
3+
`.widgets.RangeSlider` allows for creating a slider that defines
4+
a range rather than a single value.

examples/widgets/range_slider_demo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
======================================
3+
Thresholding an Image with RangeSlider
4+
======================================
5+
6+
Using the RangeSlider widget to control the thresholding of an image.
7+
"""
8+
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
from matplotlib.widgets import RangeSlider
12+
13+
fig, ax = plt.subplots()
14+
plt.subplots_adjust(left=0.25, bottom=0.25)
15+
np.random.seed(19680801)
16+
N = 128
17+
im = ax.imshow(np.random.rand(N * N).reshape(N, N))
18+
ax.margins(x=0)
19+
20+
slider_ax = plt.axes([0.20, 0.1, 0.60, 0.03])
21+
slider = RangeSlider(slider_ax, "Threshold", 0.0, 1.0)
22+
23+
24+
def update(val):
25+
im.norm.vmin = val[0]
26+
im.norm.vmax = val[1]
27+
fig.canvas.draw_idle()
28+
29+
30+
slider.on_changed(update)
31+
plt.show()
32+
33+
#############################################################################
34+
#
35+
# ------------
36+
#
37+
# References
38+
# """"""""""
39+
#
40+
# The use of the following functions, methods, classes and modules is shown
41+
# in this example:
42+
43+
import matplotlib
44+
matplotlib.widgets.RangeSlider

lib/matplotlib/tests/test_widgets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,22 @@ def test_slider_horizontal_vertical():
287287
assert_allclose(box.bounds, [0, 0, 1, 10/24])
288288

289289

290+
def test_range_slider():
291+
fig, ax = plt.subplots()
292+
plt.subplots_adjust(left=0.25, bottom=0.25)
293+
294+
slider = widgets.RangeSlider(ax=ax, label='', valmin=0., valmax=1.)
295+
296+
slider.set_val((.2, .6))
297+
assert_allclose(slider.val, (.2, .6))
298+
299+
slider.set_val((.2, .1))
300+
assert_allclose(slider.val, (.1, .2))
301+
302+
slider.set_val((-1, 10))
303+
assert_allclose(slider.val, (0, 1))
304+
305+
290306
def check_polygon_selector(event_sequence, expected_result, selections_count):
291307
"""
292308
Helper function to test Polygon Selector.

0 commit comments

Comments
 (0)