From 831be1527defd3a4806954aff10738ad7467a095 Mon Sep 17 00:00:00 2001 From: Karl Krauth Date: Thu, 17 Aug 2023 15:42:28 -0700 Subject: [PATCH 1/4] Fix facet_col and animation_frame in px.imshow for xarrays. --- .../python/plotly/plotly/express/_imshow.py | 10 ++++-- .../test_optional/test_px/test_imshow.py | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/python/plotly/plotly/express/_imshow.py b/packages/python/plotly/plotly/express/_imshow.py index 6478cc6e274..de0e22284b4 100644 --- a/packages/python/plotly/plotly/express/_imshow.py +++ b/packages/python/plotly/plotly/express/_imshow.py @@ -265,14 +265,18 @@ def imshow( if xarray_imported and isinstance(img, xarray.DataArray): dims = list(img.dims) img_is_xarray = True + pop_indexes = [] if facet_col is not None: facet_slices = img.coords[img.dims[facet_col]].values - _ = dims.pop(facet_col) + pop_indexes.append(facet_col) facet_label = img.dims[facet_col] if animation_frame is not None: animation_slices = img.coords[img.dims[animation_frame]].values - _ = dims.pop(animation_frame) + pop_indexes.append(animation_frame) animation_label = img.dims[animation_frame] + # Remove indices in sorted order. + for index in sorted(pop_indexes, reverse=True): + _ = dims.pop(index) y_label, x_label = dims[0], dims[1] # np.datetime64 is not handled correctly by go.Heatmap for ax in [x_label, y_label]: @@ -541,7 +545,7 @@ def imshow( slice_label = ( "facet_col" if labels.get("facet_col") is None else labels["facet_col"] ) - col_labels = ["%s=%d" % (slice_label, i) for i in facet_slices] + col_labels = [f"{slice_label}={i}" for i in facet_slices] fig = init_figure(args, "xy", [], nrows, ncols, col_labels, []) for attr_name in ["height", "width"]: if args[attr_name]: diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py index 09f1ae1d90f..c2e863c846b 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_imshow.py @@ -194,6 +194,40 @@ def test_imshow_xarray_slicethrough(): assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_2"])) +def test_imshow_xarray_facet_col_string(): + img = np.random.random((3, 4, 5)) + da = xr.DataArray( + img, dims=["str_dim", "dim_1", "dim_2"], coords={"str_dim": ["A", "B", "C"]} + ) + fig = px.imshow(da, facet_col="str_dim") + # Dimensions are used for axis labels and coordinates + assert fig.layout.xaxis.title.text == "dim_2" + assert fig.layout.yaxis.title.text == "dim_1" + assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_2"])) + + +def test_imshow_xarray_animation_frame_string(): + img = np.random.random((3, 4, 5)) + da = xr.DataArray( + img, dims=["str_dim", "dim_1", "dim_2"], coords={"str_dim": ["A", "B", "C"]} + ) + fig = px.imshow(da, animation_frame="str_dim") + # Dimensions are used for axis labels and coordinates + assert fig.layout.xaxis.title.text == "dim_2" + assert fig.layout.yaxis.title.text == "dim_1" + assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_2"])) + + +def test_imshow_xarray_animation_facet_slicethrough(): + img = np.random.random((3, 4, 5, 6)) + da = xr.DataArray(img, dims=["dim_0", "dim_1", "dim_2", "dim_3"]) + fig = px.imshow(da, facet_col="dim_0", animation_frame="dim_1") + # Dimensions are used for axis labels and coordinates + assert fig.layout.xaxis.title.text == "dim_3" + assert fig.layout.yaxis.title.text == "dim_2" + assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_3"])) + + def test_imshow_labels_and_ranges(): fig = px.imshow( [[1, 2], [3, 4], [5, 6]], From b35dc737bb2f584d20fe2ccd4c6a8e10e4576f04 Mon Sep 17 00:00:00 2001 From: Karl Krauth Date: Thu, 17 Aug 2023 15:52:28 -0700 Subject: [PATCH 2/4] Add changelog entry. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a0f13b6f00..36883079978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fixed issue with necessary columns from complex arguments dropped when interchanging dataframes [[#4324](https://github.com/plotly/plotly.py/pull/4324)] +- Fixed issue with px.imshow failing when facet_col is an earlier dimension than animation_frame for xarrays [[#4330](https://github.com/plotly/plotly.py/issues/4330)] +- Fixed issue with px.imshow failing when facet_col has string coordinates in xarrays [[#4329](https://github.com/plotly/plotly.py/issues/4329)] ## [5.16.0] - 2023-08-11 From b75069249ead51f6edd251d347ac90e0b0ab1d78 Mon Sep 17 00:00:00 2001 From: Karl Krauth Date: Fri, 18 Aug 2023 20:11:30 -0700 Subject: [PATCH 3/4] Move changelog entry to unreleased section. --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36883079978..cee3e089c61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## UNRELEASED + - Fixed two issues with px.imshow: [#4330] when facet_col is an earlier dimension than animation_frame for xarrays and [#4329] when facet_col has string coordinates in xarrays [#4331] + ## [5.16.1] - 2023-08-16 ### Fixed - Fixed issue with necessary columns from complex arguments dropped when interchanging dataframes [[#4324](https://github.com/plotly/plotly.py/pull/4324)] -- Fixed issue with px.imshow failing when facet_col is an earlier dimension than animation_frame for xarrays [[#4330](https://github.com/plotly/plotly.py/issues/4330)] -- Fixed issue with px.imshow failing when facet_col has string coordinates in xarrays [[#4329](https://github.com/plotly/plotly.py/issues/4329)] ## [5.16.0] - 2023-08-11 From d833fd5eccdd1fdcd38864d38ae9dd1c6f595618 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Sat, 19 Aug 2023 08:05:23 -0400 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cee3e089c61..65e02484d7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## UNRELEASED - - Fixed two issues with px.imshow: [#4330] when facet_col is an earlier dimension than animation_frame for xarrays and [#4329] when facet_col has string coordinates in xarrays [#4331] + - Fixed two issues with px.imshow: [[#4330](https://github.com/plotly/plotly.py/issues/4330)] when facet_col is an earlier dimension than animation_frame for xarrays and [[#4329](https://github.com/plotly/plotly.py/issues/4329)] when facet_col has string coordinates in xarrays [[#4331](https://github.com/plotly/plotly.py/pull/4331)] ## [5.16.1] - 2023-08-16