Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
afb5c4d
use init_figure from main px core
emmanuelle Sep 3, 2020
8be8ca0
WIP: add facet_col arg to imshow
emmanuelle Sep 3, 2020
d236bc2
animations work for grayscale images, with or without binary string
emmanuelle Sep 4, 2020
c8e852e
animations now work + tests
emmanuelle Sep 5, 2020
12cec34
docs on facets and animations + add subplots titles
emmanuelle Sep 6, 2020
ab427ae
Merge branch 'master' into imshow-animation
emmanuelle Sep 7, 2020
7a3a9f4
solved old unnoticed conflict
emmanuelle Sep 7, 2020
b689a2f
attempt to use imshow with binary strings and xarrays
emmanuelle Sep 7, 2020
fbb3f65
added test
emmanuelle Sep 7, 2020
882810f
animation work for xarrays, still need to fix slider label
emmanuelle Sep 7, 2020
ba65990
added test with xarray and animations
emmanuelle Sep 7, 2020
cf644e5
added doc
emmanuelle Sep 7, 2020
72674b7
added pooch to doc requirements
emmanuelle Sep 7, 2020
bd42385
Update packages/python/plotly/plotly/express/_imshow.py
emmanuelle Sep 8, 2020
fc2375b
Update doc/python/imshow.md
emmanuelle Sep 8, 2020
a431fad
remove commented-out code
emmanuelle Sep 9, 2020
b652039
animation + facet kinda working now, but it broke labels
emmanuelle Sep 17, 2020
59c6622
added test
emmanuelle Sep 17, 2020
c7285a3
simplified code
emmanuelle Sep 17, 2020
91c066e
simplified code
emmanuelle Sep 17, 2020
ac5aa1f
polished code and added doc example
emmanuelle Sep 17, 2020
36b9f98
Merge branch 'imshow-animation' of https://github.com/plotly/plotly.p…
emmanuelle Sep 17, 2020
8cdc6af
updated doc
emmanuelle Nov 17, 2020
cf1c2b9
Merge branch 'master' into imshow-animation
emmanuelle Nov 18, 2020
5d1d8d8
add facet_col_spacing and facet_row_spacing
emmanuelle Nov 24, 2020
c27f88a
modify error message + animation_frame label
emmanuelle Nov 24, 2020
502fdfd
improve code readibility
emmanuelle Nov 24, 2020
135b01b
added example with sequence of images
emmanuelle Nov 24, 2020
6ac3e36
typoe
emmanuelle Nov 24, 2020
a5a2252
label names
emmanuelle Nov 27, 2020
77cb5cd
label name
emmanuelle Nov 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added doc
  • Loading branch information
emmanuelle committed Sep 7, 2020
commit cf644e55f6824e29faf821b38c09dabe81ce6d23
6 changes: 4 additions & 2 deletions doc/python/imshow.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ fig.show()

*Introduced in plotly 4.11*

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.
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. For image datasets passed as xarrays, it is also possible to give an axis name as a string for `facet_col`.

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.

Expand Down Expand Up @@ -455,12 +455,14 @@ fig.show()

*Introduced in plotly 4.11*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*Introduced in plotly 4.11*
*Introduced in plotly 4.13*


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.

```python
import plotly.express as px
import xarray as xr
# Load xarray from dataset included in the xarray tutorial
ds = xr.tutorial.open_dataset('air_temperature').air[:20]
fig = px.imshow(ds, animation_frame='lat', color_continuous_scale='RdBu_r')
fig = px.imshow(ds, animation_frame='time', zmin=220, zmax=300, color_continuous_scale='RdBu_r')
fig.show()
```

Expand Down
31 changes: 19 additions & 12 deletions packages/python/plotly/plotly/express/_imshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,23 @@ def imshow(
args = locals()
apply_default_cascade(args)
labels = labels.copy()
col_labels = []
nslices = 1
if facet_col is not None:
if isinstance(facet_col, str):
facet_col = img.dims.index(facet_col)
nslices = img.shape[facet_col]
ncols = int(facet_col_wrap) if facet_col_wrap is not None else nslices
nrows = nslices // ncols + 1 if nslices % ncols else nslices // ncols
col_labels = ["plane = %d" % i for i in range(nslices)]
else:
nrows = 1
ncols = 1
if animation_frame is not None:
if isinstance(animation_frame, str):
animation_frame = img.dims.index(animation_frame)
nslices = img.shape[animation_frame]
slice_through = (facet_col is not None) or (animation_frame is not None)
plane_label = None
fig = init_figure(args, "xy", [], nrows, ncols, col_labels, [])
slice_label = None
slices = range(nslices)
# ----- Define x and y, set labels if img is an xarray -------------------
if xarray_imported and isinstance(img, xarray.DataArray):
# if binary_string:
Expand All @@ -314,13 +314,12 @@ def imshow(
# "`img.values`"
# )
dims = list(img.dims)
print(dims)
if slice_through:
slice_index = facet_col if facet_col is not None else animation_frame
slices = img.coords[img.dims[slice_index]].values
_ = dims.pop(slice_index)
plane_label = img.dims[slice_index]
slice_label = img.dims[slice_index]
y_label, x_label = dims[0], dims[1]
print(y_label, x_label)
# np.datetime64 is not handled correctly by go.Heatmap
for ax in [x_label, y_label]:
if np.issubdtype(img.coords[ax].dtype, np.datetime64):
Expand All @@ -335,8 +334,8 @@ def imshow(
labels["x"] = x_label
if labels.get("y", None) is None:
labels["y"] = y_label
if labels.get("plane", None) is None:
labels["plane"] = plane_label
if labels.get("slice", None) is None:
labels["slice"] = slice_label
if labels.get("color", None) is None:
labels["color"] = xarray.plot.utils.label_from_attrs(img)
labels["color"] = labels["color"].replace("\n", "<br>")
Expand Down Expand Up @@ -378,7 +377,7 @@ def imshow(
img = np.moveaxis(img, animation_frame, 0)
animation_frame = True
args["animation_frame"] = (
"plane" if labels.get("plane") is None else labels["plane"]
"slice" if labels.get("slice") is None else labels["slice"]
)

# Default behaviour of binary_string: True for RGB images, False for 2D
Expand Down Expand Up @@ -531,6 +530,14 @@ def imshow(
% str(img.shape)
)

# Now build figure
col_labels = []
if facet_col is not None:
slice_label = "slice" if labels.get("slice") is None else labels["slice"]
if slices is None:
slices = range(nslices)
col_labels = ["%s = %d" % (slice_label, i) for i in slices]
fig = init_figure(args, "xy", [], nrows, ncols, col_labels, [])
layout_patch = dict()
for attr_name in ["height", "width"]:
if args[attr_name]:
Expand All @@ -541,11 +548,11 @@ def imshow(
layout_patch["margin"] = {"t": 60}

frame_list = []
for index, trace in enumerate(traces):
for index, (slice_index, trace) in enumerate(zip(slices, traces)):
if facet_col or index == 0:
fig.add_trace(trace, row=nrows - index // ncols, col=index % ncols + 1)
if animation_frame:
frame_list.append(dict(data=trace, layout=layout, name=str(index)))
frame_list.append(dict(data=trace, layout=layout, name=str(slice_index)))
if animation_frame:
fig.frames = frame_list
fig.update_layout(layout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit odd to have layout and layout_patch here... it was odd before I guess but might be worth looking at merging them together earlier?

Expand Down