@@ -90,3 +90,73 @@ def imshow2(image, *args, lognorm=False, ax=None, **kwargs):
9090 if ax is None :
9191 ax = plt
9292 return ax .imshow (image , * args , ** kwargs )
93+
94+ def hyperslicer2 (arr , * args , ax = None , lognorm = False , colorbar = True , ** kwargs ):
95+ """Interactively visualize arrays of images.
96+
97+ This is a lightweight wrapper around
98+ [hyperslicer()][mpl_interactions.generic.hyperslicer] with some useful defaults:
99+
100+ - Try to set `vmin`/`vmax` to reasonable values. Note that setting
101+ `vmin`/`vmax` is incompatible with the `norm` argument, so they will only
102+ be set if `norm` is not passed.
103+ - Set `interpolation="none"`.
104+ - Enable the play buttons.
105+ - Draw a colorbar.
106+
107+ Example usage:
108+ ```python
109+ plt.figure()
110+ # Note the trailing semi-colon to swallow the return value. hyperslicer2()
111+ # returns a `controls` object by default that displays the play buttons, so
112+ # returning it from a notebook cell will end up displaying the play buttons
113+ # twice.
114+ hyperslicer2(images);
115+ ```
116+ 
117+
118+ All arguments other than the ones listed below are passed to
119+ [hyperslicer()][mpl_interactions.generic.hyperslicer], and explicitly
120+ passing any of `vmin`/`vmax`/`interpolation`/`play_buttons` will override
121+ the defaults.
122+
123+ Args:
124+ arr (array_like): The array of images to display. Should have at least
125+ three dimensions.
126+ ax (matplotlib.axes.Axes): The axis to plot the image in.
127+ lognorm (bool): Whether to display the images in a log color scale.
128+ colorbar (bool): Whether to display a colorbar.
129+ """
130+ import matplotlib .pyplot as plt
131+ from mpl_interactions import hyperslicer
132+
133+ # Enable the controls by default
134+ if "play_buttons" not in kwargs :
135+ kwargs ["play_buttons" ] = True
136+
137+ # Disable interpolation by default
138+ if "interpolation" not in kwargs :
139+ kwargs ["interpolation" ] = "none"
140+
141+ # Enable log color scale if requested and `norm` is not already set
142+ if lognorm and "norm" not in kwargs :
143+ from matplotlib .colors import LogNorm
144+ kwargs ["norm" ] = LogNorm ()
145+
146+ # Set the vmin/vmax if we're not using `norm`
147+ if "norm" not in kwargs and np .issubdtype (arr .dtype , np .number ):
148+ if "vmin" not in kwargs :
149+ kwargs ["vmin" ] = np .nanquantile (arr , 0.01 )
150+ if "vmax" not in kwargs :
151+ kwargs ["vmax" ] = np .nanquantile (arr , 0.99 )
152+
153+ if ax is None :
154+ ax = plt .gca ()
155+ fig = ax .get_figure ()
156+
157+ controls = hyperslicer (arr , * args , ax = ax , ** kwargs )
158+
159+ if colorbar :
160+ fig .colorbar (ax .get_images ()[- 1 ], ax = ax )
161+
162+ return controls
0 commit comments