Skip to content

Commit cf3eb8e

Browse files
committed
Add tests for color_continuous_scale autocolorscale behavior
Add tests to verify that user-provided color_continuous_scale overrides template autocolorscale=True, and that template autocolorscale is respected when user doesn't provide a colorscale. Changes: - tests/test_optional/test_px/test_colors.py: Add test_color_continuous_scale_autocolorscale() - tests/test_optional/test_px/test_imshow.py: Add test_imshow_color_continuous_scale_autocolorscale()
1 parent 1b1fa93 commit cf3eb8e

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

plotly/express/_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,8 +2489,7 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
24892489
# Track if color_continuous_scale was explicitly provided by user
24902490
# (before apply_default_cascade fills it from template/defaults)
24912491
user_provided_colorscale = (
2492-
"color_continuous_scale" in args
2493-
and args["color_continuous_scale"] is not None
2492+
"color_continuous_scale" in args and args["color_continuous_scale"] is not None
24942493
)
24952494
apply_default_cascade(args)
24962495

plotly/express/_imshow.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ def imshow(
236236
# Track if color_continuous_scale was explicitly provided by user
237237
# (before apply_default_cascade fills it from template/defaults)
238238
user_provided_colorscale = (
239-
"color_continuous_scale" in args
240-
and args["color_continuous_scale"] is not None
239+
"color_continuous_scale" in args and args["color_continuous_scale"] is not None
241240
)
242241
apply_default_cascade(args)
243242
labels = labels.copy()

tests/test_optional/test_px/test_colors.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,24 @@ def test_color_categorical_dtype():
6060
px.scatter(
6161
df[df.day != df.day.cat.categories[0]], x="total_bill", y="tip", color="day"
6262
)
63+
64+
65+
def test_color_continuous_scale_autocolorscale():
66+
# User-provided colorscale should override template autocolorscale=True
67+
fig = px.scatter(
68+
x=[1, 2],
69+
y=[1, 2],
70+
color=[1, 2],
71+
color_continuous_scale="Viridis",
72+
template=dict(layout_coloraxis_autocolorscale=True),
73+
)
74+
assert fig.layout.coloraxis1.autocolorscale is False
75+
76+
# Without user-provided colorscale, template autocolorscale should be respected
77+
fig2 = px.scatter(
78+
x=[1, 2],
79+
y=[1, 2],
80+
color=[1, 2],
81+
template=dict(layout_coloraxis_autocolorscale=True),
82+
)
83+
assert fig2.layout.coloraxis1.autocolorscale is None

tests/test_optional/test_px/test_imshow.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ def test_colorscale():
9898
assert fig.layout.coloraxis1.colorscale[0] == (0.0, "#440154")
9999

100100

101+
def test_imshow_color_continuous_scale_autocolorscale():
102+
# User-provided colorscale should override template autocolorscale=True
103+
fig = px.imshow(
104+
img_gray,
105+
color_continuous_scale="Viridis",
106+
template=dict(layout_coloraxis_autocolorscale=True),
107+
)
108+
assert fig.layout.coloraxis1.autocolorscale is False
109+
110+
# Without user-provided colorscale, template autocolorscale should be respected
111+
fig2 = px.imshow(
112+
img_gray,
113+
template=dict(layout_coloraxis_autocolorscale=True),
114+
)
115+
assert fig2.layout.coloraxis1.autocolorscale is None
116+
117+
101118
def test_wrong_dimensions():
102119
imgs = [1, np.ones((5,) * 3), np.ones((5,) * 4)]
103120
msg = "px.imshow only accepts 2D single-channel, RGB or RGBA images."

0 commit comments

Comments
 (0)