Title: [Bug]: Modifying Axes' position also alters the original Bbox object used for initialization · Issue #29410 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Modifying Axes' position also alters the original Bbox object used for initialization · Issue #29410 · matplotlib/matplotlib
X Title: [Bug]: Modifying Axes' position also alters the original Bbox object used for initialization · Issue #29410 · matplotlib/matplotlib
Description: Bug summary When an Axes is initialized using a Bbox object, changing the Axes' position (for example, via ax.set_position) will also alter the original Bbox object. This is because the Axes retains a reference to the Bbox object rather ...
Open Graph Description: Bug summary When an Axes is initialized using a Bbox object, changing the Axes' position (for example, via ax.set_position) will also alter the original Bbox object. This is because the Axes retain...
X Description: Bug summary When an Axes is initialized using a Bbox object, changing the Axes' position (for example, via ax.set_position) will also alter the original Bbox object. This is because the Axes re...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/29410
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Modifying Axes' position also alters the original Bbox object used for initialization","articleBody":"### Bug summary\n\nWhen an Axes is initialized using a Bbox object, changing the Axes' position (for example, via `ax.set_position`) will also alter the original Bbox object. This is because the Axes retains a reference to the Bbox object rather than creating a separate copy during initialization.\n\n### Code for reproduction\n\n```Python\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.transforms import Bbox\r\n\r\nbbox = Bbox([[0.1, 0.1], [0.9, 0.9]])\r\nfig = plt.figure()\r\nax = fig.add_axes(bbox)\r\nax.set_position([0.25, 0.25, 0.5, 0.5])\r\nprint(bbox)\r\nprint(id(bbox), id(ax._position))\n```\n\n\n### Actual outcome\n\nBbox(x0=0.25, y0=0.25, x1=0.75, y1=0.75)\r\n140507022809120 140507022809120\n\n### Expected outcome\n\nBbox(x0=0.1, y0=0.1, x1=0.9, y1=0.9)\r\n... ... # Two different IDs\n\n### Additional information\n\nThis issue originates from `_AxesBase.__init__`,\r\n\r\nhttps://github.com/matplotlib/matplotlib/blob/8d64f03a1f501ba0019279bf2f8db3930d1fe33f/lib/matplotlib/axes/_base.py#L650-L651\r\n\r\nwhere `args[0]` is directly assigned to `self._position` without making a copy.\r\n\r\nI don't know if this is a bug or a feature, but it does break encapsulation.\r\n\r\nAdditionally, some other initialization methods (for example, `Bbox.__init__`) that use `np.asarray` instead of `np.array` can cause similar issues.\n\n### Operating system\n\n_No response_\n\n### Matplotlib Version\n\n3.10.0\n\n### Matplotlib Backend\n\n_No response_\n\n### Python version\n\n_No response_\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\nNone","author":{"url":"https://github.com/hu-xiaonan","@type":"Person","name":"hu-xiaonan"},"datePublished":"2025-01-06T08:07:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/29410/matplotlib/issues/29410"}
| 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:42e3c070-476b-b73a-ad9f-08d9878e5c0f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E19E:22DC6E:288ABEE:35DC03B:6A521623 |
| html-safe-nonce | a5fbc05f9927447299ad7b948aac07a9d24e5e64901911c56c4ffbfb3effe76f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTlFOjIyREM2RToyODhBQkVFOjM1REMwM0I6NkE1MjE2MjMiLCJ2aXNpdG9yX2lkIjoiMjk3Mjg2OTUyNjc2MzQ3NjUxNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e809ad8e841835709356b870655d2746a3d3e95f7b5234615a2a396160b3bedf |
| hovercard-subject-tag | issue:2770085019 |
| 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/29410/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2737d57867dd433dc67a619c4808a24c3a05baec548fef1dec5b0a5d6c40df1b/matplotlib/matplotlib/issues/29410 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2737d57867dd433dc67a619c4808a24c3a05baec548fef1dec5b0a5d6c40df1b/matplotlib/matplotlib/issues/29410 |
| og:image:alt | Bug summary When an Axes is initialized using a Bbox object, changing the Axes' position (for example, via ax.set_position) will also alter the original Bbox object. This is because the Axes retain... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hu-xiaonan |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width