Title: [Bug]: cannot give init value for RangeSlider widget · Issue #22686 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: cannot give init value for RangeSlider widget · Issue #22686 · matplotlib/matplotlib
X Title: [Bug]: cannot give init value for RangeSlider widget · Issue #22686 · matplotlib/matplotlib
Description: Bug summary I think xy[4] = .25, val[0] should be commented in /matplotlib/widgets. py", line 915, in set_val as it prevents to initialized value for RangeSlider Code for reproduction import numpy as np import matplotlib.pyplot as plt fr...
Open Graph Description: Bug summary I think xy[4] = .25, val[0] should be commented in /matplotlib/widgets. py", line 915, in set_val as it prevents to initialized value for RangeSlider Code for reproduction import numpy ...
X Description: Bug summary I think xy[4] = .25, val[0] should be commented in /matplotlib/widgets. py", line 915, in set_val as it prevents to initialized value for RangeSlider Code for reproduction import n...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/22686
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: cannot give init value for RangeSlider widget","articleBody":"### Bug summary\r\n\r\nI think `xy[4] = .25, val[0]` should be commented in /matplotlib/widgets. py\", line 915, in set_val\r\nas it prevents to initialized value for RangeSlider\r\n\r\n### Code for reproduction\r\n\r\n```python\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.widgets import RangeSlider\r\n\r\n# generate a fake image\r\nnp.random.seed(19680801)\r\nN = 128\r\nimg = np.random.randn(N, N)\r\n\r\nfig, axs = plt.subplots(1, 2, figsize=(10, 5))\r\nfig.subplots_adjust(bottom=0.25)\r\n\r\nim = axs[0].imshow(img)\r\naxs[1].hist(img.flatten(), bins='auto')\r\naxs[1].set_title('Histogram of pixel intensities')\r\n\r\n# Create the RangeSlider\r\nslider_ax = fig.add_axes([0.20, 0.1, 0.60, 0.03])\r\nslider = RangeSlider(slider_ax, \"Threshold\", img.min(), img.max(),valinit=[0.0,0.0])\r\n\r\n# Create the Vertical lines on the histogram\r\nlower_limit_line = axs[1].axvline(slider.val[0], color='k')\r\nupper_limit_line = axs[1].axvline(slider.val[1], color='k')\r\n\r\n\r\ndef update(val):\r\n # The val passed to a callback by the RangeSlider will\r\n # be a tuple of (min, max)\r\n\r\n # Update the image's colormap\r\n im.norm.vmin = val[0]\r\n im.norm.vmax = val[1]\r\n\r\n # Update the position of the vertical lines\r\n lower_limit_line.set_xdata([val[0], val[0]])\r\n upper_limit_line.set_xdata([val[1], val[1]])\r\n\r\n # Redraw the figure to ensure it updates\r\n fig.canvas.draw_idle()\r\n\r\n\r\nslider.on_changed(update)\r\nplt.show()\r\n```\r\n\r\n\r\n### Actual outcome\r\n\r\n```python\r\n File \"\u003cipython-input-52-b704c53e18d4\u003e\", line 19, in \u003cmodule\u003e\r\n slider = RangeSlider(slider_ax, \"Threshold\", img.min(), img.max(),valinit=[0.0,0.0])\r\n\r\n File \"/Users/Vincent/opt/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/widgets.py\", line 778, in __init__\r\n self.set_val(valinit)\r\n\r\n File \"/Users/Vincent/opt/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/widgets.py\", line 915, in set_val\r\n xy[4] = val[0], .25\r\n\r\nIndexError: index 4 is out of bounds for axis 0 with size 4\r\n```\r\n\r\n### Expected outcome\r\n\r\nrange slider with user initial values\r\n\r\n### Additional information\r\n\r\nerror can be removed by commenting this line\r\n```python\r\n\r\n def set_val(self, val):\r\n \"\"\"\r\n Set slider value to *val*.\r\n\r\n Parameters\r\n ----------\r\n val : tuple or array-like of float\r\n \"\"\"\r\n val = np.sort(np.asanyarray(val))\r\n if val.shape != (2,):\r\n raise ValueError(\r\n f\"val must have shape (2,) but has shape {val.shape}\"\r\n )\r\n val[0] = self._min_in_bounds(val[0])\r\n val[1] = self._max_in_bounds(val[1])\r\n xy = self.poly.xy\r\n if self.orientation == \"vertical\":\r\n xy[0] = .25, val[0]\r\n xy[1] = .25, val[1]\r\n xy[2] = .75, val[1]\r\n xy[3] = .75, val[0]\r\n # xy[4] = .25, val[0]\r\n else:\r\n xy[0] = val[0], .25\r\n xy[1] = val[0], .75\r\n xy[2] = val[1], .75\r\n xy[3] = val[1], .25\r\n # xy[4] = val[0], .25\r\n self.poly.xy = xy\r\n self.valtext.set_text(self._format(val))\r\n if self.drawon:\r\n self.ax.figure.canvas.draw_idle()\r\n self.val = val\r\n if self.eventson:\r\n self._observers.process(\"changed\", val)\r\n\r\n```\r\n\r\n### Operating system\r\n\r\nOSX\r\n\r\n### Matplotlib Version\r\n\r\n3.5.1\r\n\r\n### Matplotlib Backend\r\n\r\n_No response_\r\n\r\n### Python version\r\n\r\n3.8\r\n\r\n### Jupyter version\r\n\r\n_No response_\r\n\r\n### Installation\r\n\r\npip","author":{"url":"https://github.com/vpicouet","@type":"Person","name":"vpicouet"},"datePublished":"2022-03-23T02:29:06.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":25},"url":"https://github.com/22686/matplotlib/issues/22686"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d79939e1-a447-9051-98e2-733cc9825043 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E6D0:29B71F:C66990:1098510:6A52A2C4 |
| html-safe-nonce | b08243f1c845743a88d2ac949800519baac17b7ab8b8f5a3d96ae4b92a00a244 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkQwOjI5QjcxRjpDNjY5OTA6MTA5ODUxMDo2QTUyQTJDNCIsInZpc2l0b3JfaWQiOiIzNTIyMzAyMDg0MjMyOTQ2MzcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 617e4d2d8c45a12b6de2bac0f664619615f500a22185ef457f4e96bde1f8c987 |
| hovercard-subject-tag | issue:1177516465 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/22686/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a311a306c5f30441e848ef817b79140c31e88b51e38de1f390e5da5980fb27d1/matplotlib/matplotlib/issues/22686 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a311a306c5f30441e848ef817b79140c31e88b51e38de1f390e5da5980fb27d1/matplotlib/matplotlib/issues/22686 |
| og:image:alt | Bug summary I think xy[4] = .25, val[0] should be commented in /matplotlib/widgets. py", line 915, in set_val as it prevents to initialized value for RangeSlider Code for reproduction import numpy ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vpicouet |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width