Skip to content

Commit 10a5541

Browse files
authored
correct zmin/zmax/range_color bug (#1981)
* correct zmin/zmax/range_color bug * black
1 parent 3f3e8eb commit 10a5541

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import plotly.graph_objs as go
22
from _plotly_utils.basevalidators import ColorscaleValidator
33
from ._core import apply_default_cascade
4-
import numpy as np # is it fine to depend on np here?
4+
import numpy as np
55

66
_float_types = []
77

@@ -107,7 +107,8 @@ def imshow(
107107
108108
range_color : list of two numbers
109109
If provided, overrides auto-scaling on the continuous color scale, including
110-
overriding `color_continuous_midpoint`.
110+
overriding `color_continuous_midpoint`. Also overrides zmin and zmax. Used only
111+
for single-channel images.
111112
112113
title : str
113114
The figure title.
@@ -147,14 +148,18 @@ def imshow(
147148

148149
# For 2d data, use Heatmap trace
149150
if img.ndim == 2:
150-
trace = go.Heatmap(z=img, zmin=zmin, zmax=zmax, coloraxis="coloraxis1")
151+
trace = go.Heatmap(z=img, coloraxis="coloraxis1")
151152
autorange = True if origin == "lower" else "reversed"
152153
layout = dict(
153154
xaxis=dict(scaleanchor="y", constrain="domain"),
154155
yaxis=dict(autorange=autorange, constrain="domain"),
155156
)
156157
colorscale_validator = ColorscaleValidator("colorscale", "imshow")
157-
range_color = range_color or [None, None]
158+
if zmin is not None and zmax is None:
159+
zmax = img.max()
160+
if zmax is not None and zmin is None:
161+
zmin = img.min()
162+
range_color = range_color or [zmin, zmax]
158163
layout["coloraxis1"] = dict(
159164
colorscale=colorscale_validator.validate_coerce(
160165
args["color_continuous_scale"]

packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py

+17
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,20 @@ def test_zmax_floats():
9797
fig = px.imshow(img)
9898
print(fig.data[0]["zmax"], zmax)
9999
assert fig.data[0]["zmax"] == None
100+
101+
102+
def test_zmin_zmax_range_color():
103+
img = img_gray / 100.0
104+
fig = px.imshow(img)
105+
assert not (fig.layout.coloraxis.cmin or fig.layout.coloraxis.cmax)
106+
fig1 = px.imshow(img, zmin=0.2, zmax=0.8)
107+
fig2 = px.imshow(img, range_color=[0.2, 0.8])
108+
assert fig1 == fig2
109+
# color_range overrides zmin and zmax
110+
fig = px.imshow(img, zmin=0.3, zmax=0.9, range_color=[0.2, 0.8])
111+
assert fig.layout.coloraxis.cmin == 0.2
112+
assert fig.layout.coloraxis.cmax == 0.8
113+
# It's possible to pass only zmin OR zmax
114+
fig = px.imshow(img, zmax=0.8)
115+
assert fig.layout.coloraxis.cmin == 0.0
116+
assert fig.layout.coloraxis.cmax == 0.8

0 commit comments

Comments
 (0)