Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct zmin/zmax/range_color bug #1981

Merged
merged 2 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 9 additions & 4 deletions packages/python/plotly/plotly/express/_imshow.py
Original file line number Diff line number Diff line change
@@ -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 = []

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,20 @@ 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.0
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.0
assert fig.layout.coloraxis.cmax == 0.8