Skip to content

Commit bc163c1

Browse files
committed
Update imshow_all
Passing unkown kwargs such as shape to imshow seems to raise an error with more recent versions of matplotlib. A good opportunity, to provide and docuemnt the keyword parameters in a more explicit way. Note I'm not to sure on the strange calculation of width for the figsize. The width gets smaller the more rows there are and multplied with 0.75 but only for more than one rows?
1 parent cf5929e commit bc163c1

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lectures/3_morphological_operations.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"metadata": {},
8787
"outputs": [],
8888
"source": [
89-
"skdemo.imshow_all(image, morphology.erosion(image, sq), shape=(1, 2))"
89+
"skdemo.imshow_all(image, morphology.erosion(image, sq))"
9090
]
9191
},
9292
{

lectures/skdemo/_skdemo.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,39 @@ def imshow_rgb_shifted(rgb_image, shift=100, ax=None):
4242
ax.set_axis_off()
4343

4444

45-
def imshow_all(*images, **kwargs):
45+
def imshow_all(
46+
*images, limits='image', titles=None, shape=None, size=5, **kwargs
47+
):
4648
""" Plot a series of images side-by-side.
4749
4850
Convert all images to float so that images have a common intensity range.
4951
5052
Parameters
5153
----------
52-
limits : str
54+
images : tuple
55+
The images to plot.
56+
limits : 'image' or 'dtype', optional
5357
Control the intensity limits. By default, 'image' is used set the
5458
min/max intensities to the min/max of all images. Setting `limits` to
5559
'dtype' can also be used if you want to preserve the image exposure.
5660
titles : list of str
5761
Titles for subplots. If the length of titles is less than the number
5862
of images, empty strings are appended.
63+
shape : tuple, optional
64+
A two-element tuple that defines the shape of the subfigure grid as
65+
(rows, columns). If not given, all `ìmages` are shown in a single row.
66+
size : int, optional
67+
Width and height of each subplot in inches.
5968
kwargs : dict
6069
Additional keyword-arguments passed to `imshow`.
6170
"""
6271
images = [img_as_float(img) for img in images]
6372

64-
titles = kwargs.pop('titles', [])
73+
if titles is None:
74+
titles = []
6575
if len(titles) != len(images):
6676
titles = list(titles) + [''] * (len(images) - len(titles))
6777

68-
limits = kwargs.pop('limits', 'image')
6978
if limits == 'image':
7079
kwargs.setdefault('vmin', min(img.min() for img in images))
7180
kwargs.setdefault('vmax', max(img.max() for img in images))
@@ -74,13 +83,13 @@ def imshow_all(*images, **kwargs):
7483
kwargs.setdefault('vmin', vmin)
7584
kwargs.setdefault('vmax', vmax)
7685

77-
nrows, ncols = kwargs.get('shape', (1, len(images)))
86+
if shape is None:
87+
shape = (1, len(images))
88+
nrows, ncols = shape
7889

79-
size = nrows * kwargs.pop('size', 5)
80-
width = size * len(images)
81-
if nrows > 1:
82-
width /= nrows * 1.33
83-
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(width, size))
90+
height = nrows * size
91+
width = ncols * size
92+
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(width, height))
8493
for ax, img, label in zip(axes.ravel(), images, titles):
8594
ax.imshow(img, **kwargs)
8695
ax.set_title(label)

0 commit comments

Comments
 (0)