Repository navigation
Cannot set colorscale in template for some trace types #3100
Description
Activity
Thanks for the report @jonmmease !
The colorscale part can be "fixed" by adding
marker.autocolorscale: falseto the template as in https://codepen.io/etpinard/pen/XxaRyB, I'm not sure if this is the desired behavior though. Perhaps we could improve the logic here:plotly.js/src/components/colorscale/defaults.js
Lines 43 to 46 in d340150
var autoColorscaleDflt; if(sclIn !== undefined) autoColorscaleDflt = !isValidScale(sclIn); coerce(prefix + 'autocolorscale', autoColorscaleDflt); var sclOut = coerce(prefix + 'colorscale'); where
autocolorscalewould default tofalseif a valid colorscale template is set.As for the colorbar part, the problem comes from the colorbar defaults call:
if(showScale) colorbarDefaults(containerIn, containerOut, layout); where
containerOutisfullData[i]for heatmap, but isfullData[i].markerfor scatter and other marker colorscales. Colorbar templates propagate correctly (usingTemplate.newContainer) fromfullData[i], but not from marker containers. Addingshould be enough to fix this, but we should look for other situations where supply-defaults subroutines are called with trace sub-containers.
Oh awesome, yeah setting
marker.autocolorscale: falsein the template takes care of the colorscale problem, which is fine with me for the time being.One more issue here. Setting
marker.autocolorscale: falsein the template causes the colorscale to be displayed properly if the template is already in place when the scatter trace is created. But if the template is applied withrelayoutafter the scatter trace is added, the template's colorscale is not applied.Reacted by etpinardThanks for pointing out the relayout issue @jonmmease (-> in https://codepen.io/etpinard/pen/gBqqgo)
I'm afraid that it points to a bigger problem though. At the moment, colorscales are mutating in the input traces and thus rely on an 'implied edit' (listed here) during
Plotly.relayout. This 'implied edit' clears thecolorscalevalue out of the input trace before callingPlots.supplyDefaultsmaking calls likerelayout(gd, 'marker.colorscale', 'Viridis')work as expected.Now there are many more attributes (e.g.
cmin/cmax,contours.*to name a few) with 'implied edits' which currently won't work withrelayout(gd, 'template', {/* */}).I'm not sure what the best way forward is. To stop mutating in things in input traces would solve the issue, or do something similar to the new histogram autobin logic, but those are fairly big projects. There might be an easier solution.
In the meantime, using
relayoutto update templates might not be the best idea.Thanks for the background @etpinard,
Would
reacthandle this case better, or would the same issues arise? It would be a bit of a project, but I think plotly.py'sFigureWidgetcould eventually move to usingreactinstead ofrestyle/relayout/update.But in the meantime, a lot of the template information does come through well when switching templates using
relayoutand I think it's a pretty useful way to explore themes. So I'd like to leave it in place for plotly.py 3.4 and just have the caveat that some template updates require the figure to be re-rendered.Would
reacthandle this case better, or would the same issues arise? INo unfortunately. Only
newPlotwill behave correctly onlayout.templateedits.- added a commit that references this issue
on Dec 17, 2018

In working on developing templates for plotly.py I've found that I'm not able to set the colorscale and colorbar properties in the template for some trace types.
This works as I expect for the
heatmaptrace type. The defualt colorscale of Viridis and the colorbar tick properties are both applied.Pen: https://codepen.io/jonmmease/pen/mzMeMW
But it does not work for the
scatter.markercolorscale and colorbar.Pen: https://codepen.io/jonmmease/pen/LgjpQG
Note that
scatter.marker.symbolis being picked up from the template, so I'm pretty sure the template specification is correct, but let me know if I'm missing something.I've noticed this same behavior for other colorscales and colorbars, and I can work through an exhaustive list later today if that's helpful. Thanks!