| 1 | [Bug]: cannot give init value for RangeSlider widget |
| 2 | ### Bug summary |
| 3 | |
| 4 | I think `xy[4] = .25, val[0]` should be commented in /matplotlib/widgets. py", line 915, in set_val |
| 5 | as it prevents to initialized value for RangeSlider |
| 6 | |
| 7 | ### Code for reproduction |
| 8 | |
| 9 | ```python |
| 10 | import numpy as np |
| 11 | import matplotlib.pyplot as plt |
| 12 | from matplotlib.widgets import RangeSlider |
| 13 | |
| 14 | # generate a fake image |
| 15 | np.random.seed(19680801) |
| 16 | N = 128 |
| 17 | img = np.random.randn(N, N) |
| 18 | |
| 19 | fig, axs = plt.subplots(1, 2, figsize=(10, 5)) |
| 20 | fig.subplots_adjust(bottom=0.25) |
| 21 | |
| 22 | im = axs[0].imshow(img) |
| 23 | axs[1].hist(img.flatten(), bins='auto') |
| 24 | axs[1].set_title('Histogram of pixel intensities') |
| 25 | |
| 26 | # Create the RangeSlider |
| 27 | slider_ax = fig.add_axes([0.20, 0.1, 0.60, 0.03]) |
| 28 | slider = RangeSlider(slider_ax, "Threshold", img.min(), img.max(),valinit=[0.0,0.0]) |
| 29 | |
| 30 | # Create the Vertical lines on the histogram |
| 31 | lower_limit_line = axs[1].axvline(slider.val[0], color='k') |
| 32 | upper_limit_line = axs[1].axvline(slider.val[1], color='k') |
| 33 | |
| 34 | |
| 35 | def update(val): |
| 36 | # The val passed to a callback by the RangeSlider will |
| 37 | # be a tuple of (min, max) |
| 38 | |
| 39 | # Update the image's colormap |
| 40 | im.norm.vmin = val[0] |
| 41 | im.norm.vmax = val[1] |
| 42 | |
| 43 | # Update the position of the vertical lines |
| 44 | lower_limit_line.set_xdata([val[0], val[0]]) |
| 45 | upper_limit_line.set_xdata([val[1], val[1]]) |
| 46 | |
| 47 | # Redraw the figure to ensure it updates |
| 48 | fig.canvas.draw_idle() |
| 49 | |
| 50 | |
| 51 | slider.on_changed(update) |
| 52 | plt.show() |
| 53 | ``` |
| 54 | |
| 55 | |
| 56 | ### Actual outcome |
| 57 | |
| 58 | ```python |
| 59 | File "<ipython-input-52-b704c53e18d4>", line 19, in <module> |
| 60 | slider = RangeSlider(slider_ax, "Threshold", img.min(), img.max(),valinit=[0.0,0.0]) |
| 61 | |
| 62 | File "/Users/Vincent/opt/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/widgets.py", line 778, in __init__ |
| 63 | self.set_val(valinit) |
| 64 | |
| 65 | File "/Users/Vincent/opt/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/widgets.py", line 915, in set_val |
| 66 | xy[4] = val[0], .25 |
| 67 | |
| 68 | IndexError: index 4 is out of bounds for axis 0 with size 4 |
| 69 | ``` |
| 70 | |
| 71 | ### Expected outcome |
| 72 | |
| 73 | range slider with user initial values |
| 74 | |
| 75 | ### Additional information |
| 76 | |
| 77 | error can be removed by commenting this line |
| 78 | ```python |
| 79 | |
| 80 | def set_val(self, val): |
| 81 | """ |
| 82 | Set slider value to *val*. |
| 83 | |
| 84 | Parameters |
| 85 | ---------- |
| 86 | val : tuple or array-like of float |
| 87 | """ |
| 88 | val = np.sort(np.asanyarray(val)) |
| 89 | if val.shape != (2,): |
| 90 | raise ValueError( |
| 91 | f"val must have shape (2,) but has shape {val.shape}" |
| 92 | ) |
| 93 | val[0] = self._min_in_bounds(val[0]) |
| 94 | val[1] = self._max_in_bounds(val[1]) |
| 95 | xy = self.poly.xy |
| 96 | if self.orientation == "vertical": |
| 97 | xy[0] = .25, val[0] |
| 98 | xy[1] = .25, val[1] |
| 99 | xy[2] = .75, val[1] |
| 100 | xy[3] = .75, val[0] |
| 101 | # xy[4] = .25, val[0] |
| 102 | else: |
| 103 | xy[0] = val[0], .25 |
| 104 | xy[1] = val[0], .75 |
| 105 | xy[2] = val[1], .75 |
| 106 | xy[3] = val[1], .25 |
| 107 | # xy[4] = val[0], .25 |
| 108 | self.poly.xy = xy |
| 109 | self.valtext.set_text(self._format(val)) |
| 110 | if self.drawon: |
| 111 | self.ax.figure.canvas.draw_idle() |
| 112 | self.val = val |
| 113 | if self.eventson: |
| 114 | self._observers.process("changed", val) |
| 115 | |
| 116 | ``` |
| 117 | |
| 118 | ### Operating system |
| 119 | |
| 120 | OSX |
| 121 | |
| 122 | ### Matplotlib Version |
| 123 | |
| 124 | 3.5.1 |
| 125 | |
| 126 | ### Matplotlib Backend |
| 127 | |
| 128 | _No response_ |
| 129 | |
| 130 | ### Python version |
| 131 | |
| 132 | 3.8 |
| 133 | |
| 134 | ### Jupyter version |
| 135 | |
| 136 | _No response_ |
| 137 | |
| 138 | ### Installation |
| 139 | |
| 140 | pip |