Title: [Bug]: Rounding to pixel boundary misaligns images in vector graphics outputs · Issue #20964 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Rounding to pixel boundary misaligns images in vector graphics outputs · Issue #20964 · matplotlib/matplotlib
X Title: [Bug]: Rounding to pixel boundary misaligns images in vector graphics outputs · Issue #20964 · matplotlib/matplotlib
Description: Bug summary When using Axes.imshow(), images are scaled to integer pixel size (_ImageBase._make_image). This makes images notably too large when using vector graphics output, misaligning them with tick marks and plotted lines. Code for r...
Open Graph Description: Bug summary When using Axes.imshow(), images are scaled to integer pixel size (_ImageBase._make_image). This makes images notably too large when using vector graphics output, misaligning them with ...
X Description: Bug summary When using Axes.imshow(), images are scaled to integer pixel size (_ImageBase._make_image). This makes images notably too large when using vector graphics output, misaligning them with ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/20964
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Rounding to pixel boundary misaligns images in vector graphics outputs","articleBody":"### Bug summary\n\nWhen using Axes.imshow(), images are scaled to integer pixel size (`_ImageBase._make_image`). This makes images notably too large when using vector graphics output, misaligning them with tick marks and plotted lines.\n\n### Code for reproduction\n\n```python\nimport numpy as np\r\nimport matplotlib as mpl\r\nimport matplotlib.pyplot as plt\r\n\r\nimg = np.zeros((100, 400))\r\nimg[::5, ::5] = 1\r\nidx = np.indices(img.shape)\r\nidx = [idx[0][::5].flatten(), idx[1][:, ::5].flatten()]\r\n\r\nfig, ax = plt.subplots(constrained_layout=True)\r\nax.imshow(img, interpolation=\"None\", origin=\"lower\")\r\nax.scatter(idx[1], idx[0], marker=\"x\", lw=0.2, s=1)\r\nfig.savefig(\"imshow.pdf\")\n```\n\n\n### Actual outcome\n\nThe image does not align with plot markers, tick marks. Towards the top-right corner, they are even placed in adjacent pixels.\r\n\r\n[Full PDF output](https://github.com/matplotlib/matplotlib/files/7091923/imshow.pdf)\r\n\n\n### Expected outcome\n\nThis can be worked around by calling `_ImageBase._make_image` with `round_to_pixel_border=False`:\r\n\r\n```python\r\nclass NotRoundedAxesImage(mpl.image.AxesImage):\r\n \"\"\"AxesImage subclass which does not round image extents to fit frame\r\n\r\n This rounding leads to improper alignment between data and image\r\n \"\"\"\r\n def make_image(self, renderer, magnification=1.0, unsampled=False):\r\n # docstring inherited\r\n trans = self.get_transform()\r\n # image is created in the canvas coordinate.\r\n x1, x2, y1, y2 = self.get_extent()\r\n bbox = mpl.transforms.Bbox(np.array([[x1, y1], [x2, y2]]))\r\n transformed_bbox = mpl.transforms.TransformedBbox(bbox, trans)\r\n clip = ((self.get_clip_box() or self.axes.bbox) if self.get_clip_on()\r\n else self.figure.bbox)\r\n # Turn off round_to_pixel_border!\r\n return self._make_image(self._A, bbox, transformed_bbox, clip,\r\n magnification, unsampled=unsampled,\r\n round_to_pixel_border=False)\r\n \r\nfig, ax = plt.subplots(constrained_layout=True)\r\n# Replicate Axes.imshow\r\nax.set_aspect(mpl.rcParams[\"image.aspect\"])\r\nai = NotRoundedAxesImage(ax, interpolation=\"none\")\r\nai.set_data(img)\r\nai.set_extent(ai.get_extent())\r\nax.add_image(ai)\r\nax.scatter(idx[1], idx[0], marker=\"x\", lw=0.2, s=1)\r\nfig.savefig(\"custom_image.pdf\")\r\n```\r\n\r\n[Full PDF output](https://github.com/matplotlib/matplotlib/files/7091982/custom_image.pdf)\r\n\n\n### Operating system\n\nUbuntu 20.04\n\n### Matplotlib Version\n\n3.4.3\n\n### Matplotlib Backend\n\nPDF, SVG, PGF, probably others\n\n### Python version\n\n3.7\n\n### Jupyter version\n\n_No response_\n\n### Other libraries\n\n_No response_\n\n### Installation\n\nconda\n\n### Conda channel\n\nconda-forge","author":{"url":"https://github.com/lschr","@type":"Person","name":"lschr"},"datePublished":"2021-09-01T14:37:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/20964/matplotlib/issues/20964"}
| 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:14b972a4-52aa-2841-b77d-b384a31ff06e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C0DE:28F29:CD7B6B:115E6D4:6A516898 |
| html-safe-nonce | 17b6bbf996e51408b207ee7b418016d000a346610dc3fe8f9569da20091989b7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMERFOjI4RjI5OkNEN0I2QjoxMTVFNkQ0OjZBNTE2ODk4IiwidmlzaXRvcl9pZCI6IjE4ODczODA4MjExNzk5ODQwMjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 31fcba893b066fea9fd96b3a8ccd4ef0373e8ebba627b47968beef475a672880 |
| hovercard-subject-tag | issue:985236363 |
| 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/20964/issue_layout |
| twitter:image | https://opengraph.githubassets.com/51e641e761e8791ee52ee157a04c4d39a9849fce4588c0838cc346c79b385e78/matplotlib/matplotlib/issues/20964 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/51e641e761e8791ee52ee157a04c4d39a9849fce4588c0838cc346c79b385e78/matplotlib/matplotlib/issues/20964 |
| og:image:alt | Bug summary When using Axes.imshow(), images are scaled to integer pixel size (_ImageBase._make_image). This makes images notably too large when using vector graphics output, misaligning them with ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lschr |
| 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 | 60eaaf6d4dbaa817031b556aebd8a0a7eeb81b97 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width