Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image with a zoom = 1 with Matplotlib imshow() (how to?)

I want to display an image (say 800x800) with Matplotlib.pyplot imshow() function but I want to display it so that one pixel of the image occupies one pixel on the screen (zoom factor = 1, no shrink, no stretch).

I'm a beginner, so do you know how to proceed?

like image 241
dom_beau Avatar asked Nov 08 '11 20:11

dom_beau


People also ask

How do I zoom in a figure in matplotlib?

If you press 'x' or 'y' while panning the motion will be constrained to the x or y axis, respectively. Press the right mouse button to zoom, dragging it to a new position. The x axis will be zoomed in proportionate to the rightward movement and zoomed out proportionate to the leftward movement.

How does matplotlib Imshow work?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .


1 Answers

Matplotlib isn't optimized for this. You'd be a bit better off with simpler options if you just want to display an image at one-pixel-to-one-pixel. (Have a look at Tkinter, for example.)

That having been said:

import matplotlib.pyplot as plt import numpy as np  # DPI, here, has _nothing_ to do with your screen's DPI. dpi = 80.0 xpixels, ypixels = 800, 800  fig = plt.figure(figsize=(ypixels/dpi, xpixels/dpi), dpi=dpi) fig.figimage(np.random.random((xpixels, ypixels))) plt.show() 

Or, if you really want to use imshow, you'll need to be a bit more verbose. However, this has the advantage of allowing you to zoom in, etc if desired.

import matplotlib.pyplot as plt import numpy as np  dpi = 80 margin = 0.05 # (5% of the width/height of the figure...) xpixels, ypixels = 800, 800  # Make a figure big enough to accomodate an axis of xpixels by ypixels # as well as the ticklabels, etc... figsize = (1 + margin) * ypixels / dpi, (1 + margin) * xpixels / dpi  fig = plt.figure(figsize=figsize, dpi=dpi) # Make the axis the right size... ax = fig.add_axes([margin, margin, 1 - 2*margin, 1 - 2*margin])  ax.imshow(np.random.random((xpixels, ypixels)), interpolation='none') plt.show() 
like image 196
Joe Kington Avatar answered Sep 28 '22 22:09

Joe Kington