From 9697458741dc05cb610902bfb193534a5a554436 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Thu, 5 Dec 2019 16:07:47 -0500 Subject: [PATCH 1/2] correct zmin/zmax/range_color bug --- .../python/plotly/plotly/express/_imshow.py | 13 +++++++++---- .../tests/test_core/test_px/test_imshow.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/python/plotly/plotly/express/_imshow.py b/packages/python/plotly/plotly/express/_imshow.py index 81c8be3a77a..6f1bf77e877 100644 --- a/packages/python/plotly/plotly/express/_imshow.py +++ b/packages/python/plotly/plotly/express/_imshow.py @@ -1,7 +1,7 @@ import plotly.graph_objs as go from _plotly_utils.basevalidators import ColorscaleValidator from ._core import apply_default_cascade -import numpy as np # is it fine to depend on np here? +import numpy as np _float_types = [] @@ -107,7 +107,8 @@ def imshow( range_color : list of two numbers If provided, overrides auto-scaling on the continuous color scale, including - overriding `color_continuous_midpoint`. + overriding `color_continuous_midpoint`. Also overrides zmin and zmax. Used only + for single-channel images. title : str The figure title. @@ -147,14 +148,18 @@ def imshow( # For 2d data, use Heatmap trace if img.ndim == 2: - trace = go.Heatmap(z=img, zmin=zmin, zmax=zmax, coloraxis="coloraxis1") + trace = go.Heatmap(z=img, coloraxis="coloraxis1") autorange = True if origin == "lower" else "reversed" layout = dict( xaxis=dict(scaleanchor="y", constrain="domain"), yaxis=dict(autorange=autorange, constrain="domain"), ) colorscale_validator = ColorscaleValidator("colorscale", "imshow") - range_color = range_color or [None, None] + if zmin is not None and zmax is None: + zmax = img.max() + if zmax is not None and zmin is None: + zmin = img.min() + range_color = range_color or [zmin, zmax] layout["coloraxis1"] = dict( colorscale=colorscale_validator.validate_coerce( args["color_continuous_scale"] diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py index 8bf1657c4e1..1eb5f5427b2 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py @@ -97,3 +97,21 @@ def test_zmax_floats(): fig = px.imshow(img) print(fig.data[0]["zmax"], zmax) assert fig.data[0]["zmax"] == None + + +def test_zmin_zmax_range_color(): + img = img_gray / 100. + fig = px.imshow(img) + assert not (fig.layout.coloraxis.cmin or + fig.layout.coloraxis.cmax) + fig1 = px.imshow(img, zmin=0.2, zmax=0.8) + fig2 = px.imshow(img, range_color=[0.2, 0.8]) + assert fig1 == fig2 + # color_range overrides zmin and zmax + fig = px.imshow(img, zmin=0.3, zmax=0.9, range_color=[0.2, 0.8]) + assert fig.layout.coloraxis.cmin == 0.2 + assert fig.layout.coloraxis.cmax == 0.8 + # It's possible to pass only zmin OR zmax + fig = px.imshow(img, zmax=0.8) + assert fig.layout.coloraxis.cmin == 0. + assert fig.layout.coloraxis.cmax == 0.8 From d719107e2d60a2746c41c0e89b108d3f10bfe26c Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Thu, 5 Dec 2019 16:28:00 -0500 Subject: [PATCH 2/2] black --- .../plotly/plotly/tests/test_core/test_px/test_imshow.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py index 1eb5f5427b2..e296a958f3a 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py @@ -100,10 +100,9 @@ def test_zmax_floats(): def test_zmin_zmax_range_color(): - img = img_gray / 100. + img = img_gray / 100.0 fig = px.imshow(img) - assert not (fig.layout.coloraxis.cmin or - fig.layout.coloraxis.cmax) + assert not (fig.layout.coloraxis.cmin or fig.layout.coloraxis.cmax) fig1 = px.imshow(img, zmin=0.2, zmax=0.8) fig2 = px.imshow(img, range_color=[0.2, 0.8]) assert fig1 == fig2 @@ -113,5 +112,5 @@ def test_zmin_zmax_range_color(): assert fig.layout.coloraxis.cmax == 0.8 # It's possible to pass only zmin OR zmax fig = px.imshow(img, zmax=0.8) - assert fig.layout.coloraxis.cmin == 0. + assert fig.layout.coloraxis.cmin == 0.0 assert fig.layout.coloraxis.cmax == 0.8