Skip to content

Commit 12cec34

Browse files
committed
docs on facets and animations + add subplots titles
1 parent c8e852e commit 12cec34

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

doc/python/imshow.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,57 @@ for compression_level in range(0, 9):
399399
fig.show()
400400
```
401401

402+
### Exploring 3-D images and timeseries with `facet_col`
403+
404+
*Introduced in plotly 4.11*
405+
406+
For three-dimensional image datasets, obtained for example by MRI or CT in medical imaging, one can explore the dataset by representing its different planes as facets. The `facet_col` argument specifies along which axes the image is sliced through to make the facets. With `facet_col_wrap` , one can set the maximum number of columns.
407+
408+
It is recommended to use `binary_string=True` for facetted plots of images in order to keep a small figure size and a short rendering time.
409+
410+
See the [tutorial on facet plots](/python/facet-plots/) for more information on creating and styling facet plots.
411+
412+
```python
413+
import plotly.express as px
414+
from skimage import io
415+
from skimage.data import image_fetcher
416+
path = image_fetcher.fetch('data/cells.tif')
417+
data = io.imread(path)
418+
img = data[25:40]
419+
fig = px.imshow(img, facet_col=0, binary_string=True, facet_col_wrap=5, height=700)
420+
fig.show()
421+
```
422+
423+
```python
424+
import plotly.express as px
425+
from skimage import io
426+
from skimage.data import image_fetcher
427+
path = image_fetcher.fetch('data/cells.tif')
428+
data = io.imread(path)
429+
img = data[25:40]
430+
fig = px.imshow(img, facet_col=0, binary_string=True, facet_col_wrap=5)
431+
# To have square facets one needs to unmatch axes
432+
fig.update_xaxes(matches=None)
433+
fig.update_yaxes(matches=None)
434+
fig.show()
435+
```
436+
437+
### Exploring 3-D images and timeseries with `animation_frame`
438+
439+
*Introduced in plotly 4.11*
440+
441+
For three-dimensional image datasets, obtained for example by MRI or CT in medical imaging, one can explore the dataset by sliding through its different planes in an animation. The `animation_frame` argument of `px.imshow` sets the axis along which the 3-D image is sliced in the animation.
442+
443+
```python
444+
import plotly.express as px
445+
from skimage import io
446+
from skimage.data import image_fetcher
447+
path = image_fetcher.fetch('data/cells.tif')
448+
data = io.imread(path)
449+
img = data[25:40]
450+
fig = px.imshow(img, animation_frame=0, binary_string=True)
451+
fig.show()
452+
```
453+
402454
#### Reference
403455
See https://plotly.com/python/reference/#image for more information and chart attribute options!

packages/python/plotly/plotly/express/_imshow.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,17 @@ def imshow(
288288
args = locals()
289289
apply_default_cascade(args)
290290
labels = labels.copy()
291+
col_labels = []
291292
if facet_col is not None:
292293
nslices = img.shape[facet_col]
293-
ncols = int(facet_col_wrap)
294+
ncols = int(facet_col_wrap) if facet_col_wrap is not None else nslices
294295
nrows = nslices // ncols + 1 if nslices % ncols else nslices // ncols
296+
col_labels = ["plane = %d" % i for i in range(nslices)]
295297
else:
296298
nrows = 1
297299
ncols = 1
298-
fig = init_figure(args, "xy", [], nrows, ncols, [], [])
300+
slice_through = (facet_col is not None) or (animation_frame is not None)
301+
fig = init_figure(args, "xy", [], nrows, ncols, col_labels, [])
299302
# ----- Define x and y, set labels if img is an xarray -------------------
300303
if xarray_imported and isinstance(img, xarray.DataArray):
301304
if binary_string:
@@ -353,16 +356,13 @@ def imshow(
353356

354357
# --------------- Starting from here img is always a numpy array --------
355358
img = np.asanyarray(img)
356-
slice_through = False
357359
if facet_col is not None:
358360
img = np.moveaxis(img, facet_col, 0)
359361
facet_col = True
360-
slice_through = True
361362
if animation_frame is not None:
362363
img = np.moveaxis(img, animation_frame, 0)
363364
animation_frame = True
364365
args["animation_frame"] = "plane"
365-
slice_through = True
366366

367367
# Default behaviour of binary_string: True for RGB images, False for 2D
368368
if binary_string is None:

0 commit comments

Comments
 (0)