Skip to content

Image being snapped erroneously #1591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented Dec 12, 2012

Since 1262326 implemented by @mdboom the following example results in a figure which has a 1 pixel border around left and bottom edges in the resultant image:

import matplotlib.pyplot as plt
import matplotlib

import numpy


if __name__ == '__main__':
    print matplotlib.__version__

    f = plt.figure(figsize=[1, 1])
    ax = plt.axes([0, 0, 1, 1],
                  frameon=False,
                  )

    data = numpy.tile(numpy.arange(12), 15).reshape(20, 9)

    im = ax.imshow(data, origin='upper',
                   extent=[-10, 10, -10, 10], interpolation='none',
                   cmap='gray'
                   )

    x = y = 2
    ax.set_xlim([-x, x])
    ax.set_ylim([-y, y])

    ax.set_xticks([])
    ax.set_yticks([])

    fname = 'test.png'
    plt.savefig(fname, facecolor=(0, 1, 0))
    plt.close()

    im = plt.imread(fname)
    r, g, b, a = sum(im[:, 0])
    print 'Left edge: ', r, g, b, a
    r, g, b, a = sum(im[:, -1])

    print 'Right edge:' , r, g, b, a
    assert g != 100, 'Expected a non-green edge - but sadly, it was.'

output

Notice the green 1px border on the bottom and right of the output.

The problem is limited to the Agg backend and the "none" image interpolation scheme.

With a bit of refinement, this example should be turned into a test once we have a fix for this.

@pelson
Copy link
Member Author

pelson commented Dec 12, 2012

Based on the changes in the linked commit I changed the following lines from:

        rasterizer.clip_box(std::max(int(floor(l - 0.5)), 0),
                            std::max(int(floor(height - b - 0.5)), 0),
                            std::min(int(floor(r - 0.5)), int(width)),
                            std::min(int(floor(height - t - 0.5)), int(height)));

to

        rasterizer.clip_box(std::max(int(floor(l - 0.5)), 0),
                            std::max(int(height - floor(b - 0.5)), 0),
                            std::min(int(ceil(r + 0.5)), int(width)),
                            std::min(int(height - ceil(t + 0.5)), int(height)));

Attaining this was completely unscientific and all I know is that it "fixed" my plot. @mdboom any thoughts?

@mdboom
Copy link
Member

mdboom commented Dec 12, 2012

The extra pixel is of course dependent on the size of the figure. I update your example above so it tries a bunch of different sizes, and indeed it fails on some of the sizes with master, but doesn't fail on any with your suggested change. That seems good enough for me, but I would want to run it over the test suite, too. I'll attach a pull request here and let Travis have a look.

@mdboom
Copy link
Member

mdboom commented Dec 12, 2012

I'll wait for Travis to run over this, but we should also add a unit test based on your example before merging.

@pelson
Copy link
Member Author

pelson commented Dec 12, 2012

Wow.... Ok, there are loads of failures with that change. Do you want to look into those or would you like me to?

@mdboom
Copy link
Member

mdboom commented Dec 12, 2012

I may not have a chance today or tomorrow -- the failures probably require a manual look -- it may be that we're actually improving them with this change and the reference files need to be updated -- (but I haven't looked at the results yet, so who knows...)

@efiring
Copy link
Member

efiring commented Feb 19, 2013

Is this related to #800?

@mdboom
Copy link
Member

mdboom commented Feb 20, 2013

They are somewhat related. I think we should at least investigate whether #800 resolves this issue.

@mdboom
Copy link
Member

mdboom commented Mar 1, 2013

Having finally looked at the image differences, I think this is not the right solution (at least as it stands). It causes the clipping box to be larger than the axes box (in some cases) and the content bleeds over:

errorbar_zorder

And I think it's ultimately the lack of an axes box in the example of this issue that reveals the problem. I'm going to look at #800, and also investigate some other approaches for this issue.

mdboom added a commit to mdboom/matplotlib that referenced this pull request Mar 1, 2013
… (see there for further discussion) and matplotlib#1591.  This, unlike matplotlib#800, doesn't change the snapping in a fundamental way, but it does help with text/image/marker alignment of snapped and non-snapped objects.
mdboom added a commit to mdboom/matplotlib that referenced this pull request Mar 1, 2013
@mdboom
Copy link
Member

mdboom commented Mar 22, 2013

I believe this is fixed by #1800. @pelson, feel free to reopen if you disagree.

@mdboom mdboom closed this Mar 22, 2013
@pelson
Copy link
Member Author

pelson commented Mar 22, 2013

Yep. Thanks @mdboom.

@mdboom mdboom deleted the image_edge branch November 10, 2015 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants