Skip to content

Misalignment imshow vs. grid lines #1441

Closed

Description

@rougier

The following script show a mis-alignment between grid lines and image patterns while they should be aligned:

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

n = 16
fig = plt.figure(figsize=(6,6))
Z = np.zeros((n,n))
Z[::2,::2] = Z[1::2,1::2] = 1
plt.imshow(Z, interpolation='none', cmap=plt.cm.gray, extent=[0,n,0,n], alpha=0.25)
plt.xticks(np.arange(0,n), []), plt.yticks(np.arange(0,n), [])
plt.grid(ls='solid')
delta = 0.01
plt.xlim(1-delta,1+delta), plt.ylim(1-delta,1+delta)
plt.savefig('pylab-grid.png')
plt.savefig('pylab-grid.pdf')

Activity

  1. pelson commented on Oct 31, 2012

    @pelson
    Member

    @rougier : You can make code blocks with three consecutive ``` (grave/backquotes). Hope you don't mind but I've edited your description to use them.

  2. tacaswell commented on Jun 3, 2014

    @tacaswell
    Member

    If you interactively pan around on this example the amount that the lines miss the edges of the pixels by jumps around. I suspect that this might be related to #3057 (more beating?) and smells like snap is related, but I don't understand that well enough

  3. tacaswell commented on Jun 4, 2014

    @tacaswell
    Member

    Punting this to 1.4.x as this is only visible if you zoom way in and looks like it is something subtle down at the bottom (maybe not a fast find or fix).

  4. modified the milestones: v1.4.x, v1.4.0 on Jun 4, 2014
  5. mojca commented on Jun 4, 2014

    @mojca

    I'm not objecting the increase of the milestone. I agree that it might be difficult to debug this and that one would usually not see the difference, I just wanted to note that this effect may be visible at normal magnifications as well. I spotted it at relatively normal scale when noticing that the grid was off the pixels on the image (my real-life example is very similar to #2935).

    What is the expected result of the example above? I'm asking because I see off-by-one error on PNG, but the PDF I get seems completely wrong.

  6. modified the milestones: v1.4.x, on Feb 7, 2015
  7. jkseppan commented on Jul 8, 2015

    @jkseppan
    Member

    If I change interpolation='none' to interpolation='nearest' the PDF looks similar to the PNG. The Agg backend has to resample images to display them, but the PDF backend does not, so when you make a very low-resolution image it's up to the viewer program to figure out how to display it.

    Here's another example adapted from #2935:

    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams['pdf.compression'] = 0
    
    plt.gcf().set_size_inches([1,1])
    a=np.bitwise_xor.outer(np.arange(20)&2, np.arange(20)&2)
    im = plt.imshow(a, interpolation='none', cmap='gray', origin='lower', extent=[-2,2,-2,2])
    plt.gca().set_xticks(np.linspace(-2,2,21))
    plt.gca().set_yticks(np.linspace(-2,2,21))
    plt.gca().xaxis.set_ticklabels([])
    plt.gca().yaxis.set_ticklabels([])
    plt.grid(ls='-',lw=0.1,color='white')
    plt.axis('square')
    plt.savefig('pattern.png')
    plt.savefig('pattern.pdf')
    

    Here's how the checkerboard pattern image looks, as written in the PDF file:

    ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00
    ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00
    00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fe
    00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff ff
    fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 00
    ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00
    00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fe
    00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff ff
    fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 00
    ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00
    00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fe
    00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff ff
    fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 00
    ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00
    00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fe
    00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff ff
    fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 00
    ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00
    00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fc 00 01 fe fe
    00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff fe 00 00 ff ff
    

    Note that the values are not all 00 or ff. Even with interpolation='none' there is some resampling going on, and the data written in the file is not exactly what was given to imshow.

  8. modified the milestones: , on Jul 17, 2015
  9. efiring commented on May 22, 2016

    @efiring
    Member

    The bug is still present after the image refactor.

  10. 14 remaining items

  11. github-actions commented on Mar 1, 2024

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  12. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Apr 1, 2024
  13. pelson commented on Apr 1, 2024

    @pelson
    Member

    Just to add that this issue still exists on main, and I can confirm that it can be seen without the Agg renderer (cairo & another). This supports the assumption that this is a problem in the image code, rather than the renderer.

    I find it curious that you only see the misalignment in the y-axis, and not in the x.

  14. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Apr 1, 2024
  15. timhoffm commented on Apr 1, 2024

    @timhoffm
    Member

    This should stay open.

  16. QuLogic commented on Jan 15, 2026

    @QuLogic
    Member

    This should be fixed by #30184.

  17. removed
    keepItems to be ignored by the “Stale” Github Action
    on Jan 15, 2026
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