Title: [Bug]: .contains for some artists does not check for visibility · Issue #23875 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: .contains for some artists does not check for visibility · Issue #23875 · matplotlib/matplotlib
X Title: [Bug]: .contains for some artists does not check for visibility · Issue #23875 · matplotlib/matplotlib
Description: Bug summary I recently managed to find the source of a long-standing bug in my application that was quite tricky to find. It turned out to be a consequence of the contains method of a line returning true even when the line was set to not...
Open Graph Description: Bug summary I recently managed to find the source of a long-standing bug in my application that was quite tricky to find. It turned out to be a consequence of the contains method of a line returnin...
X Description: Bug summary I recently managed to find the source of a long-standing bug in my application that was quite tricky to find. It turned out to be a consequence of the contains method of a line returnin...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/23875
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: .contains for some artists does not check for visibility","articleBody":"### Bug summary\n\nI recently managed to find the source of a long-standing bug in my application that was quite tricky to find.\r\n\r\nIt turned out to be a consequence of the `contains` method of a line returning `true` even when the line was set to not visible at the time. Although from what I can see in the documentation there is nothing prohibiting this, I think almost everyone would want `contains` to return false for objects which have been plotted and set to invisible.\r\n\r\nMoreover, a non-exhaustive look through the code shows that there are class-specific checks in place for visibility for some objects, but not others. Specifically, the following do not have checks:\r\n\r\n```\r\nlines.Line2D\r\npatches.Patch\r\nquiver.QuiverKey\r\n```\r\n\r\nBut these ones do (though not explicitly documented.)\r\n\r\n```\r\ncollections.Collection\r\ntext.Text\r\nimage.BboxImage\r\n```\r\n\r\nIt seems like a fix might put the check for visibility in the `_default_contains` method of `Artist`. This is called by the child implementations, but as of now seems to provide only a bail-out if the event is outside the figure canvas. Although I guess maybe it was done that way if there are some specific exceptions where you would actually want `contains` to return true (maybe bounding boxes or more abstract things not plotted directly).\n\n### Code for reproduction\n\n```python\nfrom matplotlib import pyplot as plt\r\n\r\nclass Demo:\r\n\r\n def __init__(self):\r\n\r\n fig, ax = plt.subplots()\r\n\r\n self.lines = []\r\n self.lines.append(ax.axhline(1))\r\n self.lines.append(ax.axhline(-1))\r\n self.lines[0].set_visible(False)\r\n\r\n self.vis_points = ax.scatter([-0.5, 0.5], [0, 0])\r\n self.invis_points = ax.scatter([0], [0])\r\n self.invis_points.set_visible(False)\r\n self.points = [self.vis_points, self.invis_points]\r\n\r\n fig.canvas.mpl_connect('button_press_event', self.clicked)\r\n\r\n plt.show()\r\n\r\n def clicked(self, event):\r\n at_obj = lambda x: x.contains(event)[0]\r\n\r\n if any(at_obj(l) for l in self.lines):\r\n print(f\"Clicked on a line at {event.ydata:0.3f}\")\r\n elif any(at_obj(p) for p in self.points):\r\n print(f\"Clicked point at {event.ydata:0.3f}\")\r\n else:\r\n print('Clicked elsewhere')\r\n\r\nif __name__ == '__main__':\r\n demo = Demo()\n```\n\n\n### Actual outcome\n\nIt is possible to click anywhere near the invisible line at y=1 and see click registered message.\r\nHowever, clicking at [0,0], the location of an invisible scatter point does not show a point clicked message (expected behavior).\n\n### Expected outcome\n\nClicking anywhere y=1 should not generate a line click message --- just as clicking at 0,0 does not generate a point clicked message.\n\n### Additional information\n\n_No response_\n\n### Operating system\n\nArch linux, Linux mint, Windows\n\n### Matplotlib Version\n\n3.5.0\n\n### Matplotlib Backend\n\nqt\n\n### Python version\n\n3.9.9\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\n_No response_","author":{"url":"https://github.com/rdgraham","@type":"Person","name":"rdgraham"},"datePublished":"2022-09-11T22:04:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/23875/matplotlib/issues/23875"}
| 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:3befbb31-3e1f-5b93-d186-0657202c900a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF06:B2444:52E55BE:73E12FB:6A510504 |
| html-safe-nonce | ffa1d706a3d33c754f989cdbe06efce917263e04b65fcc1d6525e86e4bafb541 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjA2OkIyNDQ0OjUyRTU1QkU6NzNFMTJGQjo2QTUxMDUwNCIsInZpc2l0b3JfaWQiOiI0NDQ0MzQwMTU3MTkwMDQ2OTgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e5331509ec5ac98ca1379edbb1fcf2a7c2380fac10df228e02e901bf7a5a9d12 |
| hovercard-subject-tag | issue:1369077076 |
| 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/23875/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d35218484007549ba0f9c4177707107cea0c7ab2205fc86d1e2c9623c1455c34/matplotlib/matplotlib/issues/23875 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d35218484007549ba0f9c4177707107cea0c7ab2205fc86d1e2c9623c1455c34/matplotlib/matplotlib/issues/23875 |
| og:image:alt | Bug summary I recently managed to find the source of a long-standing bug in my application that was quite tricky to find. It turned out to be a consequence of the contains method of a line returnin... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rdgraham |
| hostname | github.com |
| expected-hostname | github.com |
| None | 38906a4da4e3e9fd88dfeacee232f5932c0c5f7495de3b4efc53ddfac7173d12 |
| 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 | 90441ea9b98e7491aa6ebcd135fccce4e2396f48 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width