Skip to content
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

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

Closed
breedlun opened this issue Apr 11, 2014 · 8 comments
Closed

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

breedlun opened this issue Apr 11, 2014 · 8 comments

Comments

@breedlun
Copy link
Contributor

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,

none

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,

none 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')

nearest pdf

@breedlun
Copy link
Contributor Author

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.

@tacaswell
Copy link
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?

@breedlun
Copy link
Contributor Author

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

@tacaswell
Copy link
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.

@breedlun
Copy link
Contributor Author

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

Nearest_vs_none-png

Nearest_vs_none-pdf

"""
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')

@breedlun
Copy link
Contributor Author

@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.

@tacaswell
Copy link
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.

@breedlun
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants