Problem
When a Plotly template sets coloraxis_autocolorscale=True, the color_continuous_scale argument passed to plotly.express functions is ignored.
Reproduction
import plotly.express as px
import plotly.graph_objects as go
df = px.data.gapminder().query("year == 2007")
template = go.layout.Template(layout=go.Layout(coloraxis_autocolorscale=True))
# User specifies colorscale - THIS IS IGNORED
fig = px.choropleth(
df,
locations="iso_alpha",
color="lifeExp",
color_continuous_scale=["red", "yellow", "green"],
template=template,
)
fig.show() # Renders with auto-detected scale, not red-yellow-green
Root cause
As per the plotly docs, when autocolorscale=True, the colorscale is auto-detected based on the data, ignoring any explicit colorscale setting (this itself is quite confusing behaviour I think, but that's a separate issue - let's assume for now it should be left with that logic):
In case colorscale is unspecified or autocolorscale is True, the default palette will be chosen according to whether numbers in the color array are all positive, all negative or mixed.
In the following code autocolorscale is not set on coloraxis1. This means the template's autocolorscale=True is inherited, causing the user's color_continuous_scale to be completely ignored:
|
layout_patch["coloraxis1"] = dict( |
|
colorscale=colorscale_validator.validate_coerce( |
|
args["color_continuous_scale"] |
|
), |
|
cmid=args["color_continuous_midpoint"], |
|
cmin=range_color[0], |
|
cmax=range_color[1], |
|
colorbar=dict( |
|
title_text=get_decorated_label(args, args[colorvar], colorvar) |
|
), |
Proposed fix
When color_continuous_scale is explicitly provided, set autocolorscale=False on coloraxis1 to ensure the user's colorscale is respected. I can raise a PR for this if you like!
Problem
When a Plotly template sets
coloraxis_autocolorscale=True, thecolor_continuous_scaleargument passed to plotly.express functions is ignored.Reproduction
Root cause
As per the plotly docs, when
autocolorscale=True, the colorscale is auto-detected based on the data, ignoring any explicitcolorscalesetting (this itself is quite confusing behaviour I think, but that's a separate issue - let's assume for now it should be left with that logic):In the following code
autocolorscaleis not set oncoloraxis1. This means the template'sautocolorscale=Trueis inherited, causing the user'scolor_continuous_scaleto be completely ignored:plotly.py/plotly/express/_core.py
Lines 2707 to 2716 in f083977
Proposed fix
When
color_continuous_scaleis explicitly provided, setautocolorscale=Falseoncoloraxis1to ensure the user's colorscale is respected. I can raise a PR for this if you like!