Text of discourse.matplotlib.org › t › odd-behavior-with-nearest-interpolation › 19559 as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

odd behavior with 'nearest' interpolation - matplotlib-users - Matplotlib

odd behavior with 'nearest' interpolation Community matplotlib-users _Smit_Christine_E_GS November 17, 2015, 10:22pm 1 Hi! I am using matplotlib v 1.4.3 with Python 2.7.10 :: Anaconda 2.4.0 (64-bit). What I am doing here is creating a 7x7 pixel image from a 7x7 matrix. I expect to see one pixel per data point, but that's not what I'm seeing. Instead of a diagonal make up of single pixels, I get an odd 2x2 pixel blob in the middle of the correct one pixel diagonal. ··· --------------------------------------------------------------------------- import numpy as np import matplotlib.pylab as plt if __name__ == "__main__": n = 7 data = np.identity(n, float) # Create an nxn size figure with no frame fig = plt.figure(figsize=(n, n), frameon=False) # make the axes to the edge of the figure ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0]) # turn the axes off ax.set_axis_off() # add the axes to this figure fig.add_axes(ax) # show the data.

Don't do any interpolation. ax.imshow(data, interpolation='nearest', origin='lower',aspect='auto') # Save the figure at 1 dot per inch, which should mean 1 data point per # pixel fig.savefig("image.png", dpi=1) --------------------------------------------------------------------------- Since I'm not sure that if I can attach the png image I get, here is a ppm version of the image I get (between the ------). Save this image.ppm minus the dashes and you should be able to open it in something like gimp. --------------------------------------------------------------------------- P3 # CREATOR: GIMP PNM Filter Version 1.1 7 7 255 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 --------------------------------------------------------------------------- Thanks.

Christine -------------- next part -------------- An HTML attachment was scrubbed... URL: < http://mail.python.org/pipermail/matplotlib-users/attachments/20151117/9da59709/attachment.html > nathan12343 November 17, 2015, 10:38pm 2 This seems to be working ok for me: ··· On Tue, Nov 17, 2015 at 4:22 PM, Smit, Christine E. (GSFC-610.2)[TELOPHASE CORP] <christine.e.smit at nasa.gov> wrote: Hi! I am using matplotlib v 1.4.3 with Python 2.7.10 :: Anaconda 2.4.0 (64-bit).

What I am doing here is creating a 7x7 pixel image from a 7x7 matrix. I expect to see one pixel per data point, but that's not what I'm seeing. Instead of a diagonal make up of single pixels, I get an odd 2x2 pixel blob in the middle of the correct one pixel diagonal. --------------------------------------------------------------------------- import numpy as np import matplotlib.pylab as plt if __name__ == "__main__": n = 7 data = np.identity(n, float) # Create an nxn size figure with no frame fig = plt.figure(figsize=(n, n), frameon=False) # make the axes to the edge of the figure ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0]) # turn the axes off ax.set_axis_off() # add the axes to this figure fig.add_axes(ax) # show the data.

Don't do any interpolation. ax.imshow(data, interpolation='nearest', origin='lower',aspect='auto') # Save the figure at 1 dot per inch, which should mean 1 data point per # pixel fig.savefig("image.png", dpi=1) --------------------------------------------------------------------------- Since I'm not sure that if I can attach the png image I get, here is a ppm version of the image I get (between the ------). Save this image.ppm minus the dashes and you should be able to open it in something like gimp. --------------------------------------------------------------------------- P3 # CREATOR: GIMP PNM Filter Version 1.1 7 7 255 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 --------------------------------------------------------------------------- Thanks.

Christine _______________________________________________ Matplotlib-users mailing list Matplotlib-users at python.org Matplotlib-users Info Page -------------- next part -------------- An HTML attachment was scrubbed... URL: < http://mail.python.org/pipermail/matplotlib-users/attachments/20151117/dcc347ec/attachment.html&gt ; jenshnielsen November 18, 2015, 11:28am 3 I can confirm this. The issue is notable with a dpi lower than 10 or so and seems to get worse as it is lowered towards 1.

Can you try plotting the image with interpolation='none' If I do that I get the correct behaviour. 'none' is probably the correct setting if you wish to match image matrix 1to1 to png coords anyway. @nathan The image in the notebook is plotted with a different dpi and works correctly. best Jens ··· On Tue, 17 Nov 2015 at 22:38 Nathan Goldbaum <nathan12343 at gmail.com> wrote: This seems to be working ok for me: Untitled3.ipynb · GitHub On Tue, Nov 17, 2015 at 4:22 PM, Smit, Christine E. (GSFC-610.2)[TELOPHASE > CORP] <christine.e.smit at nasa.gov> wrote: Hi!

I am using matplotlib v 1.4.3 with Python 2.7.10 :: Anaconda 2.4.0 (64-bit). What I am doing here is creating a 7x7 pixel image from a 7x7 matrix. I expect to see one pixel per data point, but that's not what I'm seeing. Instead of a diagonal make up of single pixels, I get an odd 2x2 pixel blob in the middle of the correct one pixel diagonal. --------------------------------------------------------------------------- import numpy as np import matplotlib.pylab as plt if __name__ == "__main__": n = 7 data = np.identity(n, float) # Create an nxn size figure with no frame fig = plt.figure(figsize=(n, n), frameon=False) # make the axes to the edge of the figure ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0]) # turn the axes off ax.set_axis_off() # add the axes to this figure fig.add_axes(ax) # show the data.

Don't do any interpolation. ax.imshow(data, interpolation='nearest', origin='lower',aspect='auto') # Save the figure at 1 dot per inch, which should mean 1 data point per # pixel fig.savefig("image.png", dpi=1) --------------------------------------------------------------------------- Since I'm not sure that if I can attach the png image I get, here is a ppm version of the image I get (between the ------). Save this image.ppm minus the dashes and you should be able to open it in something like gimp. --------------------------------------------------------------------------- P3 # CREATOR: GIMP PNM Filter Version 1.1 7 7 255 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 127 0 0 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 0 0 127 --------------------------------------------------------------------------- Thanks.

Christine _______________________________________________ Matplotlib-users mailing list Matplotlib-users at python.org Matplotlib-users Info Page _______________________________________________ Matplotlib-users mailing list Matplotlib-users at python.org Matplotlib-users Info Page -------------- next part -------------- An HTML attachment was scrubbed... URL: < http://mail.python.org/pipermail/matplotlib-users/attachments/20151118/133c6249/attachment-0001.html&gt ; _Smit_Christine_E_GS November 19, 2015, 2:25pm 4 Yes.

It works with 'none.' The problem is that sometimes we need to create images with low inflation factors. So, our data is nxm data point

The rest of this page is on the live site.