| | """ |
| | ====================================== |
| | Thresholding an Image with RangeSlider |
| | ====================================== |
| | |
| | Using the RangeSlider widget to control the thresholding of an image. |
| | |
| | The RangeSlider widget can be used similarly to the `.widgets.Slider` |
| | widget. The major difference is that RangeSlider's ``val`` attribute |
| | is a tuple of floats ``(lower val, upper val)`` rather than a single float. |
| | |
| | See :doc:`/gallery/widgets/slider_demo` for an example of using |
| | a ``Slider`` to control a single float. |
| | |
| | See :doc:`/gallery/widgets/slider_snap_demo` for an example of having |
| | the ``Slider`` snap to discrete values. |
| | """ |
| |
|
| | import matplotlib.pyplot as plt |
| | import numpy as np |
| |
|
| | from matplotlib.widgets import RangeSlider |
| |
|
| | |
| | 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') |
| |
|
| | |
| | slider_ax = fig.add_axes([0.20, 0.1, 0.60, 0.03]) |
| | slider = RangeSlider(slider_ax, "Threshold", img.min(), img.max()) |
| |
|
| | |
| | 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): |
| | |
| | |
| |
|
| | |
| | im.norm.vmin = val[0] |
| | im.norm.vmax = val[1] |
| |
|
| | |
| | lower_limit_line.set_xdata([val[0], val[0]]) |
| | upper_limit_line.set_xdata([val[1], val[1]]) |
| |
|
| | |
| | fig.canvas.draw_idle() |
| |
|
| |
|
| | slider.on_changed(update) |
| | plt.show() |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|