Skip to content

Commit 56d912f

Browse files
Merge pull request #2746 from plotly/imshow-animation
Add facet_col and animation_frame argument to imshow
2 parents f03ed59 + 77cb5cd commit 56d912f

File tree

4 files changed

+319
-42
lines changed

4 files changed

+319
-42
lines changed

doc/python/imshow.md

+91-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.4.2
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.7
23+
version: 3.7.3
2424
plotly:
2525
description: How to display image data in Python with Plotly.
2626
display_as: scientific
@@ -399,6 +399,95 @@ for compression_level in range(0, 9):
399399
fig.show()
400400
```
401401

402+
### Exploring 3-D images, timeseries and sequences of images with `facet_col`
403+
404+
*Introduced in plotly 4.14*
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 axis the image is sliced through to make the facets. With `facet_col_wrap`, one can set the maximum number of columns. For image datasets passed as xarrays, it is also possible to specify the axis by its name (label), thus passing a string to `facet_col`.
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[20:45:2]
419+
fig = px.imshow(img, facet_col=0, binary_string=True, facet_col_wrap=5)
420+
fig.show()
421+
```
422+
423+
Facets can also be used to represent several images of equal shape, like in the example below where different values of the blurring parameter of a Gaussian filter are compared.
424+
425+
```python
426+
import plotly.express as px
427+
import numpy as np
428+
from skimage import data, filters, img_as_float
429+
img = data.camera()
430+
sigmas = [1, 2, 4]
431+
img_sequence = [filters.gaussian(img, sigma=sigma) for sigma in sigmas]
432+
fig = px.imshow(np.array(img_sequence), facet_col=0, binary_string=True,
433+
labels={'facet_col':'sigma'})
434+
# Set facet titles
435+
for i, sigma in enumerate(sigmas):
436+
fig.layout.annotations[i]['text'] = 'sigma = %d' %sigma
437+
fig.show()
438+
```
439+
440+
```python
441+
print(fig)
442+
```
443+
444+
### Exploring 3-D images and timeseries with `animation_frame`
445+
446+
*Introduced in plotly 4.14*
447+
448+
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.
449+
450+
```python
451+
import plotly.express as px
452+
from skimage import io
453+
from skimage.data import image_fetcher
454+
path = image_fetcher.fetch('data/cells.tif')
455+
data = io.imread(path)
456+
img = data[25:40]
457+
fig = px.imshow(img, animation_frame=0, binary_string=True)
458+
fig.show()
459+
```
460+
461+
### Animations of xarray datasets
462+
463+
*Introduced in plotly 4.14*
464+
465+
For xarray datasets, one can pass either an axis number or an axis name to `animation_frame`. Axis names and coordinates are automatically used for the labels, ticks and animation controls of the figure.
466+
467+
```python
468+
import plotly.express as px
469+
import xarray as xr
470+
# Load xarray from dataset included in the xarray tutorial
471+
ds = xr.tutorial.open_dataset('air_temperature').air[:20]
472+
fig = px.imshow(ds, animation_frame='time', zmin=220, zmax=300, color_continuous_scale='RdBu_r')
473+
fig.show()
474+
```
475+
476+
### Combining animations and facets
477+
478+
It is possible to view 4-dimensional datasets (for example, 3-D images evolving with time) using a combination of `animation_frame` and `facet_col`.
479+
480+
```python
481+
import plotly.express as px
482+
from skimage import io
483+
from skimage.data import image_fetcher
484+
path = image_fetcher.fetch('data/cells.tif')
485+
data = io.imread(path)
486+
data = data.reshape((15, 4, 256, 256))[5:]
487+
fig = px.imshow(data, animation_frame=0, facet_col=1, binary_string=True)
488+
fig.show()
489+
```
490+
402491
#### Reference
403492

404493
See [function reference for `px.(imshow)`](https://plotly.com/python-api-reference/generated/plotly.express.imshow) or https://plotly.com/python/reference/image/ for more information and chart attribute options!

doc/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ pyarrow
2828
cufflinks==0.17.3
2929
kaleido
3030
umap-learn
31+
pooch
3132
wget

0 commit comments

Comments
 (0)