Skip to content

Instantly share code, notes, and snippets.

@dwf
Created April 12, 2012 15:50
Show Gist options
  • Save dwf/2368502 to your computer and use it in GitHub Desktop.
Save dwf/2368502 to your computer and use it in GitHub Desktop.
"""
A matplotlib-based image viewer utility function.
"""
import matplotlib.pyplot as plt
def image_view(arr, fig=None, frame_on=True, *args, **kwargs):
"""
Sensibly display an image in a plot window.
Parameters
----------
arr : array_like
Array-like parameter matching the input format of `imshow`.
fig : object, optional
A matplotlib `Figure` instance, or `None` to use `gcf()`.
frame_on : boolean, optional
Whether to place a frame around the axes object (default is `True`).
All remaining positional and keyword arguments are passed along to
`imshow`. The `interpolation` keyword argument is added as 'nearest'
if it is not specified either via keywords or positionally.
Returns
-------
img : object
A `matplotlib.image.AxesImage` object as returned by `imshow`.
"""
if fig is None:
fig = plt.gcf()
# Create an Axes that fills the whole figure.
ax = plt.Axes(fig, [0, 0, 1, 1], yticks=[], xticks=[], frame_on=frame_on)
fig.delaxes(fig.gca())
fig.add_axes(ax)
# Throw in an `interpolation` keyword argument in order to turn off
# the antialiasing.
if 'interpolation' not in kwargs and len(args) < 4:
kwargs['interpolation'] = 'nearest'
return ax.imshow(arr, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment