views:

85

answers:

2

I am new to matplotlib and python and would like to display an image so that 1 pixel of the image is actually represented by 1 pixel in the figure. In MATLAB, this is achieved with the command truesize(). How can I do this in Python?

I tried playing around with the imshow() arguments as well as set_dpi() and set_figwidth()/set_figheight(), but with no luck.

Thanks.

+1  A: 

If you want to create images right down to the pixel level, why not use PIL in the first place? That way you wouldn't have to programatically calculate your true drawing area by substracting margins, labels and axis widths from the figure extend.

honk
A: 

This hack does what I wanted to do, though it's still not perfect:

h = mplt.imshow(img, interpolation='nearest')

dpi = h.figure.get_dpi()
h.figure.set_figwidth(img.shape[0] / dpi)
h.figure.set_figheight(img.shape[1] / dpi)
h.figure.canvas.resize(img.shape[1] + 1, img.shape[0] + 1)

h.axes.set_position([0, 0, 1, 1])
h.axes.set_xlim(-1, img.shape[1])
h.axes.set_ylim(img.shape[0], -1)

It can be generalized to account for a margin around the axes holding the image.

Lucas