Title: imshow padding around NaN values · Issue #18735 · matplotlib/matplotlib · GitHub
Open Graph Title: imshow padding around NaN values · Issue #18735 · matplotlib/matplotlib
X Title: imshow padding around NaN values · Issue #18735 · matplotlib/matplotlib
Description: There seems to be a discrepancy in how matplotlib's imshow displays rasters which contain NaN areas. The NaN areas of the raster appear larger than they really are. I assume the problem might be that the raw raster gets resampled/interpo...
Open Graph Description: There seems to be a discrepancy in how matplotlib's imshow displays rasters which contain NaN areas. The NaN areas of the raster appear larger than they really are. I assume the problem might be th...
X Description: There seems to be a discrepancy in how matplotlib's imshow displays rasters which contain NaN areas. The NaN areas of the raster appear larger than they really are. I assume the problem might b...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/18735
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"imshow padding around NaN values","articleBody":"There seems to be a discrepancy in how matplotlib's `imshow` displays rasters which contain NaN areas. The NaN areas of the raster appear larger than they really are.\r\n\r\nI assume the problem might be that the raw raster gets resampled/interpolated during display (but _before_ converting to RGBA) and the NaN values propagate during that interpolation (since the ordinary mean of a list containing at least one nonfinite value is also not finite). If so, a fix would be to ensure conversion to RGBA always precedes any image resampling/rescaling.\r\n\r\nHere is an example. The first subplot shows a float32 raster with values of zero and one. The second subplot exactly replaces the nonzero pixels with NaN, however the resulting shapes look different (making narrow non-NaN features invisible). The third subplot combines both, showing that the non-NaN area (red) is _eroded_, resulting in a black outline. \r\n\r\nThe expected correct behaviour would be that the red layer perfectly covers the black layer, leaving no border visible.\r\n\r\n```python\r\nimport matplotlib.pyplot as plt, numpy as np\r\n\r\nraster = np.full(shape=[200]*2, fill_value=np.nan, dtype=np.float32) # example image\r\nraster[100:150, 100:150] = 0 \r\nfor i in range(10):\r\n raster[20*i:20*i+i, :] = 0 # lines of varying thickness\r\n \r\nplt.figure(figsize=(8,8)) # small figure\r\n\r\nplt.subplot(1,3,1).imshow(np.isnan(raster).astype(np.float32)) # 0 vs 1\r\nplt.subplot(1,3,2).imshow(raster) # 0 vs NaN\r\n \r\nax = plt.subplot(1,3,3)\r\nax.imshow(np.isnan(raster), cmap='gray') # black border/underlayer\r\nax.imshow(raster, cmap='autumn') # red interior\r\n```\r\n\r\n\r\n\r\n\r\nTested on matplotlib 3.2.1, in a jupyter 1.0.0 sandbox using `ipykernel.pylab.backend_inline` backend, ipykernel 5.3.4, python 3.6.9, Linux 4.14 amzn2 x86_64.\r\n","author":{"url":"https://github.com/benjimin","@type":"Person","name":"benjimin"},"datePublished":"2020-10-15T04:00:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":25},"url":"https://github.com/18735/matplotlib/issues/18735"}
| 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:d0e181cc-0baa-c51b-4b7f-8c3158e8f488 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C24C:117213:595B6B:76B9F5:6A5303A1 |
| html-safe-nonce | 9dcf751bd826b7c8d9727b895d4a7bb39affcbe76e5f4c5d40b518ca39fa9dd0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjRDOjExNzIxMzo1OTVCNkI6NzZCOUY1OjZBNTMwM0ExIiwidmlzaXRvcl9pZCI6Ijg3MjYzMjIxMzQ5NjY3OTcyMTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 12f3da1da173d460ad2b87530d15c4f49c15f6a468142cdd6a6c2414f0ce83e3 |
| hovercard-subject-tag | issue:721952600 |
| 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/18735/issue_layout |
| twitter:image | https://opengraph.githubassets.com/df33b93fd6fc7afdc01d366db2736e36fe3479df8a0722f6bdce07fdee0a5864/matplotlib/matplotlib/issues/18735 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/df33b93fd6fc7afdc01d366db2736e36fe3479df8a0722f6bdce07fdee0a5864/matplotlib/matplotlib/issues/18735 |
| og:image:alt | There seems to be a discrepancy in how matplotlib's imshow displays rasters which contain NaN areas. The NaN areas of the raster appear larger than they really are. I assume the problem might be th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | benjimin |
| 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