Skip to content

Commit

Permalink
Allow axes to be provided to imshow and fix colorbar
Browse files Browse the repository at this point in the history
Allow axes to be provided to imshow and fix colorbar

Allow axes to be provided to imshow and fix colorbar

Allow axes to be provided to imshow and fix colorbar

Use tight layout by default

Try and fix appveyor issue

Handle deprecation warning on appveyor

Pin the colorbar to the image using box-forced

Fix tests again

Update function signature

Update function signature

Fix imshow call

Fix docstring with utf characters

Remove kwargs
  • Loading branch information
blink1073 committed Dec 13, 2015
1 parent 8b0d511 commit 3e4203b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
22 changes: 16 additions & 6 deletions skimage/io/_plugins/matplotlib_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import warnings
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from ...util import dtype as dtypes
from ...exposure import is_low_contrast
from ...util.colormap import viridis
Expand Down Expand Up @@ -110,7 +111,7 @@ def _get_display_range(image):
return lo, hi, cmap


def imshow(im, *args, **kwargs):
def imshow(im, ax=None, show_cbar=None, **kwargs):
"""Show the input image and return the current axes.
By default, the image is displayed in greyscale, rather than
Expand All @@ -131,8 +132,11 @@ def imshow(im, *args, **kwargs):
----------
im : array, shape (M, N[, 3])
The image to display.
*args, **kwargs : positional and keyword arguments
ax: `matplotlib.axes.Axes`, optional
The axis to use for the image, defaults to plt.gca().
show_cbar: boolean, optional.
Whether to show the colorbar (used to override default behavior).
**kwargs : Keyword arguments
These are passed directly to `matplotlib.pyplot.imshow`.
Returns
Expand All @@ -147,9 +151,15 @@ def imshow(im, *args, **kwargs):
kwargs.setdefault('cmap', cmap)
kwargs.setdefault('vmin', lo)
kwargs.setdefault('vmax', hi)
ax_im = plt.imshow(im, *args, **kwargs)
if cmap != _default_colormap:
plt.colorbar()

ax = ax or plt.gca()
ax_im = ax.imshow(im, **kwargs)
if (cmap != _default_colormap and show_cbar is not False) or show_cbar:
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(ax_im, cax=cax)
ax.set_adjustable('box-forced')
ax.get_figure().tight_layout()
return ax_im

imread = plt.imread
Expand Down
5 changes: 3 additions & 2 deletions skimage/io/tests/test_mpl_imshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_low_dynamic_range():

def test_outside_standard_range():
plt.figure()
with expected_warnings(["out of standard range"]):
with expected_warnings(["out of standard range|CObject type is marked"]):
ax_im = io.imshow(im_hi)
assert ax_im.get_clim() == (im_hi.min(), im_hi.max())
assert n_subplots(ax_im) == 2
Expand All @@ -87,7 +87,8 @@ def test_outside_standard_range():

def test_nonstandard_type():
plt.figure()
with expected_warnings(["Low image dynamic range"]):

with expected_warnings(["Low image dynamic range|CObject type is marked"]):
ax_im = io.imshow(im64)
assert ax_im.get_clim() == (im64.min(), im64.max())
assert n_subplots(ax_im) == 2
Expand Down

0 comments on commit 3e4203b

Please sign in to comment.