FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

aliasing with imshow(z, interpolation = 'none'), when saved as a pdf · Issue #2972 · matplotlib/matplotlib · GitHub

Repository navigation

aliasing with imshow(z, interpolation = 'none'), when saved as a pdf #2972

Description

I am finding that matplotlib v1.3.0 will blur the edges of an image's pixels when I use interpolation = 'none' and save the image as a pdf. For example, the following code

z = np.array([[0.25, 0.75, 1.0, 0.75], [0.1, 0.65, 0.5, 0.4], [0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]])
fig1 = plt.figure()
ax = plt.subplot()
ax.imshow(z, interpolation = 'none')
plt.savefig('none.png')

produces the following png,

which looks just like the matplotlib figure window, as it should. However, when I save the figure as a pdf

plt.savefig('none.pdf')

I get the following pdf,

Perhaps this is the intended mode of operation, but IMHO the output file should look like the figure window. Fortunately, there is a work around. If use interpolation = 'nearest', everything is hunky dory:

fig2 = plt.figure()
ax = plt.subplot()
ax.imshow(z, interpolation = 'nearest')
plt.savefig('nearest.pdf')

Activity

  1. breedlun commented on Apr 11, 2014

    ContributorAuthor

    Alright, now I see why the pdf backend behaves differently with interpolation = 'nearest' vs interpolation = 'none'. When I take a large image and scale it down to fit in a small space, interpolation = 'none' preserves the resolution of the image, while interpolation = 'nearest' does nearest neighbor interpolation to the nearest pixel, which reduces image resolution. Backends that only produce images do not have this distinction. They just take whatever is on the screen and stick it in an image file, which means interpolation = 'none' and interpolation = 'nearest' are equivalent.

    So, I retract my earlier statement that interpolation = 'none' in the pdf backend should make the pdf look like the figure window. I like that interpolation = 'none' and interpolation = 'nearest' are different in the pdf backend. Perhaps there should be an example on matplotlib.org that shows this difference, so other users do not get confused like me.

  2. tacaswell commented on Apr 11, 2014

    Member

    @stretch97 I agree it would be useful to have that documentation (this is something I actually don't know a whole lot about). Sense you have already done the leg work of understanding what is going on, would you mind drafting that documentation?

  3. breedlun commented on Apr 11, 2014

    ContributorAuthor

    Sure. I could put together a draft, as long as someone more knowledgable than me reviews it before we post it.

  4. tacaswell commented on Apr 11, 2014

    Member

    Don't worry, it will get reviewed before it gets merged.

    The review process will go more smoothly if you put the edits in a pull request.

  5. breedlun commented on Apr 12, 2014

    ContributorAuthor

    @tacaswell Here is what I put together for an example. Others can feel free to edit this.

    """
    Displays the difference between interpolation = 'none' and 
    interpolation = 'nearest'.
    
    The two types of interpolation are equivalent when converting a figure to an 
    image file, such as a PNG.  Interpolation = 'none' and 
    interpolation = 'nearest' behave quite differently, however, when converting a 
    figure to a vector graphics file, such as a PDF.  As shown, 
    Interpolation = 'none' works well when a big image is scaled down, while 
    interpolation = 'nearest' works well when a small image is blown up.
    """
    
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    
    #Load big image
    big_im = mpimg.imread('big_image.png')
    #Define small image
    small_im = np.array([[0.25, 0.75, 1.0, 0.75], [0.1, 0.65, 0.5, 0.4], \
        [0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]])
    
    #Create a 2x2 table of plots
    fig = plt.figure(figsize = [8.0, 7.5])
    ax = plt.subplot(2,2,1)
    ax.imshow(big_im, interpolation = 'none')
    ax = plt.subplot(2,2,2)
    ax.imshow(big_im, interpolation = 'nearest')
    ax = plt.subplot(2,2,3)
    ax.imshow(small_im, interpolation = 'none')
    ax = plt.subplot(2,2,4)
    ax.imshow(small_im, interpolation = 'nearest')
    plt.subplots_adjust(left = 0.24, wspace = 0.2, hspace = 0.1, \
        bottom = 0.05, top = 0.86)
    
    #Label the rows and columns of the table
    fig.text(0.03, 0.645, 'Big Image\nScaled Down', ha = 'left')
    fig.text(0.03, 0.225, 'Small Image\nBlown Up', ha = 'left')
    fig.text(0.383, 0.90, "Interpolation = 'none'", ha = 'center')
    fig.text(0.75, 0.90, "Interpolation = 'nearest'", ha = 'center')
    
    #Save as a png and as a pdf
    txt = fig.text(0.452, 0.95, 'Saved as a PNG', fontsize = 18)
    plt.savefig('Nearest_vs_none.png', bbox_inches = 'tight')
    txt.set_text('Saved as a PDF')
    plt.savefig('Nearest_vs_none.pdf', bbox_inches = 'tight')
    
  6. breedlun commented on Apr 14, 2014

    ContributorAuthor

    @tacaswell What do you think of my example? I am just a matplotlib user, not a matplotlib developer, so would it be easy enough for you to put it "in a pull request" for me? I can send you the "big_image.png" file.

  7. tacaswell commented on Apr 14, 2014

    Member

    @stretch97 The example looks nice, however it is far easier for me if you put it in ;)

    I assure you almost all of the current devs started as 'just users'. If you create the PR the commits will have your name on them so you get credit for your contributions. It also makes it easy for you to respond to feed back. If it is your PR then you can just add a new commit and push it to github, if it is my PR then you have to get me to make and push any changes.

    I would suggest you add your example code to the gallery (the png's will be auto-generated) and add a few sentences to the docstring of imshow explaining the consequences of 'nearest' vs 'no' interpolation.

    See http://matplotlib.org/devel/gitwash/git_development.html If you are a mpl user, then you are writing code. If you are writing code using version control is essential, so learning git is a good idea anyway.

  8. breedlun commented on Apr 18, 2014

    ContributorAuthor

    Now that I have submitted a pull request (breedlun#1), this issue can be closed. The conversation can continue at the pull request.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL