Title: [Bug]: pdf export for large image sizes results in wrong colors · Issue #25806 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: pdf export for large image sizes results in wrong colors · Issue #25806 · matplotlib/matplotlib
X Title: [Bug]: pdf export for large image sizes results in wrong colors · Issue #25806 · matplotlib/matplotlib
Description: Bug summary Exporting a figure filled with imshow as pdf is using the wrong colors if the image size/resolution becomes large: E.g. gray cells become red. Code for reproduction import numpy as np import matplotlib.pyplot as plt # data: f...
Open Graph Description: Bug summary Exporting a figure filled with imshow as pdf is using the wrong colors if the image size/resolution becomes large: E.g. gray cells become red. Code for reproduction import numpy as np i...
X Description: Bug summary Exporting a figure filled with imshow as pdf is using the wrong colors if the image size/resolution becomes large: E.g. gray cells become red. Code for reproduction import numpy as np i...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/25806
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: pdf export for large image sizes results in wrong colors","articleBody":"### Bug summary\n\nExporting a figure filled with imshow as pdf is using the wrong colors if the image size/resolution becomes large: E.g. gray cells become red.\n\n### Code for reproduction\n\n```python\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n# data: four background tiles with one of them highlighted\r\nbackground = [0.9]*3 # gray\r\nhighlight = [1,0,0] # red\r\nX = np.array([[background,highlight],[background,background]]) # varying colors and combinations gives a rich spectrum of bug-phenotypes\r\n\r\n# big figure\r\ndpi, figsize = 500, 20 # less dpi or figsize avoids the bug\r\nfig,ax = plt.subplots(figsize=(figsize,figsize),dpi=dpi)\r\nax.imshow(X)\r\n\r\n# export it\r\nfig.savefig(f'minimal.png') # shows correctly three tiles in gray and one tile in red\r\nfig.savefig(f'minimal.pdf') # shows incorrectly all four tiles in red\n```\n\n\n### Actual outcome\n\nThe png export shows the correct colors: 3 quarters gray, 1 quarter red. The pdf export shows incorrect colors: 4 quarters red.\r\n\r\n[minimal.pdf](https://github.com/matplotlib/matplotlib/files/11370977/minimal.pdf)\r\n\n\n### Expected outcome\n\nThe png and pdf export shows the correct colors: 3 quarters gray, 1 quarter red. I.e. the pdf should also look like the png pasted above.\n\n### Additional information\n\nThis bug only appears for large figure sizes and dpi (maybe if figuresize * dpi is larger than some threshold).\r\nIt is also dependent on the colors chosen and the content of the image. In my real-world use case I have a large heatmap with many fields and one color is consistently replaced by some other color from the plot. I also observed variations which replace a color with a darker version of it, e.g. if one switches the roles of red and gray in the minimal example above: Then instead of red the background is dark red in the pdf.\r\n\r\nThis minimal example was executed in a fresh conda env with matplotlib as only dependency. I tried it before with matplotlib 3.6.3 on a less clean environment with the same results. I dont have an example of it working in previous versions.\r\n\r\nI guess it is an issue with a buffer of fixed size in the pdf backend, as it works with the png backend.\r\n\r\nA workaround can be the reduction of the dpi - for vector graphics this still contains all the information. But it changes the \"physical\" size of the pdf.\r\n\n\n### Operating system\n\nRHEL 7.9\n\n### Matplotlib Version\n\n3.7.1\n\n### Matplotlib Backend\n\n_No response_\n\n### Python version\n\n3.11.3\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\nconda","author":{"url":"https://github.com/simonwm","@type":"Person","name":"simonwm"},"datePublished":"2023-05-02T08:23:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/25806/matplotlib/issues/25806"}
| 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:8a84ef55-ff60-d0d8-35d4-d38334081825 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EAA4:71E1B:548870:6FD57E:6A5189DF |
| html-safe-nonce | eb4ad59c7f7938482773afc0da82b43324aba214e4f7f980127826d7c16a0a34 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQUE0OjcxRTFCOjU0ODg3MDo2RkQ1N0U6NkE1MTg5REYiLCJ2aXNpdG9yX2lkIjoiNTczOTk2NjU1NDQxMTQ2OTI3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d282d374d5ca3a9319bf32c3834872eeb7f293e57766a4829c4b49ed59da4480 |
| hovercard-subject-tag | issue:1691989078 |
| 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/25806/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9bed4e32f52d27ac53e9a14a4a764bea4e37e655b5cb771217922297910e6055/matplotlib/matplotlib/issues/25806 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9bed4e32f52d27ac53e9a14a4a764bea4e37e655b5cb771217922297910e6055/matplotlib/matplotlib/issues/25806 |
| og:image:alt | Bug summary Exporting a figure filled with imshow as pdf is using the wrong colors if the image size/resolution becomes large: E.g. gray cells become red. Code for reproduction import numpy as np i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | simonwm |
| 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 | cb77a7b8b06cd86c243be33fd8e7922d70a9d243 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width