Skip to content

imshow pixel boundaries wrong when zoomed in #14143

Closed

Description

@plisdku

Bug report

Bug summary

imshow pixel boundaries visibly shift from expected coordinates by many pixels when zooming in, depending on dpi but not improving at high dpi.

I am trying to plot gridded data with imshow so it aligns with other plot elements like lines. When I zoom in on the image so image pixels are bigger than the plot axes, the boundaries between pixels are visibly shifted from where they ought to be. The shift depends on the dpi of the figure but cannot be resolved by changing the dpi.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

dpi=72
plt.figure(figsize=(12,4), dpi=dpi)
z = np.eye(2)
for ii, d in enumerate([1, .1, .01]):
    plt.subplot(1,3,ii+1)
    plt.imshow(z)
    plt.xlim([0.5-d, 0.5+d])
    plt.ylim([0.5-d, 0.5+d])
    plt.vlines([-.5, 0.5, 1.5, 2.5, 3.5], -.5, 3.5, 'r')
    plt.hlines([-.5, 0.5, 1.5, 2.5, 3.5], -.5, 3.5, 'r')
    plt.title([f"Full image ({dpi} dpi)", "10x", "100x"][ii])

Actual outcome

Several results with different dpi. Screenshotted at same resolution because one of them was generated at 1000 dpi.

image

Expected outcome

The red grid lines drawn with vlines and hlines should lie directly on pixel boundaries.

Matplotlib version

Mac OS X 10.12.6

Matplotlib version 3.0.3 installed from pip I think.
Backend: module://ipykernel.pylab.backend_inline

Python 3.7

Activity

  1. jklymak commented on May 6, 2019

    @jklymak
    Member

    Does dpi=1000 really change the dpi in the inline backend?

  2. plisdku commented on May 6, 2019

    @plisdku
    Author

    It definitely gave me an enorrrrrrrmous png file, I can attest to that. :-) I don't know how mpl is working on the inside though.

  3. added this to the v3.2.0 milestone on May 6, 2019
  4. jklymak commented on May 6, 2019

    @jklymak
    Member

    Yes, I agree, that certainly is a bug... Thanks for the good report.

  5. tacaswell commented on May 6, 2019

    @tacaswell
    Member

    This is yet more issues with your favorite part of the code base @jklymak (AxesImage ;) )

    I think this is a duplicate of #1441 I still think the issue is that we are doing the re-sampling from the full input data -> the screen. If you zoom way in / turn up the DPI, we start to get numerical issues mapping back from screen space to input space.

    Put another way, when you are very zoomed in we are pulling a 200 x 200 window out of a 200k x 200k notional array and the error on picking the right place to transition from input pixel A to pixel B should be evaluated against the 200K space.

  6. modified the milestones: v3.2.0, v3.3.0 on Sep 5, 2019
  7. adeak commented on Feb 11, 2020

    @adeak
    Contributor

    I'm pretty sure this is the same issue, but I can see the same bug even without zooming in with this gridded example (on a TkAgg backend in case it matters):

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig,ax = plt.subplots() 
    N = 12 
    ax.imshow(np.random.rand(N, N)) 
    seps = np.arange(-0.5, N) 
    ax.vlines(seps, -0.5, N - 0.5, linewidth=1)
    ax.hlines(seps, -0.5, N - 0.5, linewidth=1)
    plt.show()

    mpl_imshow_grid_bug
    Note the bunch of yellow and brighter green pixels that stretch over the grid lines.

    The key difference with respect to the original example in the issue is probably that the thinner grid lines that I use no longer hide the misalignment. Just like in the suspected duplicate some of the overlaps jump around if I pan interactively, but this is not very obvious because most of the overlaps are pretty resilient to panning and only disappear in a narrow window during panning.

  8. modified the milestones: v3.3.0, v3.4.0 on May 7, 2020
  9. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  10. removed this from the v3.5.0 milestone on Sep 25, 2021
  11. 8 remaining items

  12. added
    status: inactive
    on Aug 29, 2025
  13. adeak commented on Aug 29, 2025

    @adeak
    Contributor

    The code block from my previous comment still shows the bug on v3.10.5 (with qtagg backend here):

    Image

    This should stay open.

  14. added
    keep
    and removed
    status: inactive
    on Aug 29, 2025
  15. QuLogic commented on Jan 15, 2026

    @QuLogic
    Member

    This should be fixed by #30184.

  16. removed
    keep
    on Jan 15, 2026
  17. adeak commented on Jan 15, 2026

    @adeak
    Contributor

    @QuLogic I'm not completely sure that I'm testing it right but I think the bug in my last comment is still present on main. I couldn't get an editable install to work right now, so I ended up just installing current main from github directly (plus PyQt6). I see this:

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig,ax = plt.subplots() 
    N = 12 
    ax.imshow(np.random.rand(N, N)) 
    seps = np.arange(-0.5, N) 
    ax.vlines(seps, -0.5, N - 0.5, linewidth=1)
    ax.hlines(seps, -0.5, N - 0.5, linewidth=1)
    plt.show()
    Image

    The grid issue is still there. And I think I'm correctly on main:

    >>> import matplotlib
    >>> matplotlib.__version__
    '3.11.0.dev1652+gddd4196c7'

    as newest commit on main currently is ddd4196.

  18. QuLogic commented on Jan 16, 2026

    @QuLogic
    Member

    Your example is different from the original issue as it very much has to do with zooming. Perhaps @ayshih has some ideas on yours.

  19. ayshih commented on Jan 16, 2026

    @ayshih
    Contributor

    @adeak: Argh, you are correct that your issue has not been fixed. I've re-confirmed that #30184 fixed the original issue, but I apparently overlooked checking your example too. I'll look into it.

  20. adeak commented on Jan 16, 2026

    @adeak
    Contributor

    Sorry @ayshih 😅 For what it's worth this is not an issue for me today, I found this 6 years ago. So if this is a different problem we can open a dedicated issue to track it and you need not jump into it just for my sake ;)

  21. ayshih commented on Jan 21, 2026

    @ayshih
    Contributor

    @adeak: Based on my investigation so far, the problem in your example is indeed distinct from the original issue, and it is very important to fix. It's not a bug internal to the image-resampling code, but rather it has to do with how image placement affects what resampling is requested and how the resampled image is drawn. Can you create a dedicated issue?

  22. adeak commented on Jan 21, 2026

    @adeak
    Contributor

    Sure thing @ayshih, thanks for looking into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions