You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
Plotly Express does not play well with templates #70
I noticed that Plotly Express is quite opinionated with some default values, which unfortunately do not play well with template's defined values such as graph height or color scale.
For example, I would expect px.histogram(some_dataframe, template="seaborn") to use the seaborn color palette.
Is there any plans to pick up default values in the active template instead?
Plotly Express indeed opts out of the template color scale, because it sets colors explicitly. You can now (as of v0.1.8 which came out minutes ago!) set the px.defaults.color_continuous_scale and px.defaults.color_discrete_sequence at the top of a file and it will use those for the rest of the session though. (We could look at having PX read the template for the color scale and sequence but there are a lot of places that can be specified and the precedence rules are implemented in JS not Python so it's tricky).
By default, Plotly Express sets layout.height to 600 but this can now also be overridden by setting px.defaults.height (and setting that to None should cause it to use the underlying template's height in principle although I haven't tested it).
Wow, thanks for the quick response 🥇
Even though reading the colorscale directly from the template would perfectly fit my needs (I have a bunch of JSON serialized Figure to convert to a custom template directly in a Dash app), I will give it a try.
Closing for now, but I might reopen this issue.
I recently shuffled things up such that the defaults are no longer passed in from outside the core, but are set internally if the arguments are None: 9726dd5
Right now the default defaults are hardcoded at the top of _core.py but... I could just set those to None and add yet another layer of defaulting that would basically do "if None then try to look inside the current setting of template to grab some colors/heights". (and I would leave the default template to plotly so this would mean we'd still use the right colors, other than falling back to Plotly rather than the Plasma I hardcoded today 🙈 )
if None then try to look inside the current setting of template to grab some colors/heights
That makes sense to me @nicolaskruchten. We should also double-check
setting that to None should cause it to use the underlying template's height in principle although I haven't tested it
It would be nice if there was a logical correspondence between the px template and a user-specified template, so any px default parameter set to None has a template fallback.
I wonder which parameters (here, right?) wouldn't play well with template defaults. Do you forsee any problems with color_discrete_map (even though it's not a default)? That's gonna override color_discrete_sequence no matter what, right?
there are a lot of places [the template] can be specified
Which places besides layout.template? I'm guessing you'd have to crawl for other template parameters?
It would be nice if there was a logical correspondence between the px template and a user-specified template, so any px default parameter set to None has a template fallback.
The px defaults are very few: color scale and sequence, symbol/dash sequences and dimensions. Everything else is use-specified. Among those, color scale&sequence and width/height are the only ones that are present in the template.
there are a lot of places [the template] can be specified
Actually "[the template]" is not what I meant, I meant the colors :) In a template, we have color scales and sequences in a few places in layout such as layout.colorway, layout.piecolorway, layout.sunburstcolorway, layout.colorscale.sequential, layout.colorscale.sequentialminus, layout.colorscale.divergingbut also on a per-trace-type basis: a template can specify one colorscale for scatter and another for heatmaps, and it can also specify that the first scatter is one color and the second is another, at which point I think that scatter traces will end up alternating between the two colors. So... yeah, there are a lot of ways to specify colors in a template.
All that said, px could just ignore most of that complexity and try to read from template.layout.colorway and template.layout.colorscale.sequential and call it a day :)
@Batalex if you want to take a look at or play with #74 I'd be curious to know what you think.
I played with the height thing and no matter what I do, there's no way I can get height from template to impact the resulting figure, so I suspect there's something in the way we display in a notebook which prevents this from happening but I'm not sure what. As a result #74 leaves the default 600 in and you'd have to set the default to None to let the template shine through.
@Batalex this is now released! You should be able to set pio.templates.default and plotly_express will grab the colors from there, and it also respects px.defaults.template as well as the template kwarg.
Activity
nicolaskruchten commentedon May 7, 2019
Plotly Express indeed opts out of the template color scale, because it sets colors explicitly. You can now (as of v0.1.8 which came out minutes ago!) set the
px.defaults.color_continuous_scaleandpx.defaults.color_discrete_sequenceat the top of a file and it will use those for the rest of the session though. (We could look at having PX read the template for the color scale and sequence but there are a lot of places that can be specified and the precedence rules are implemented in JS not Python so it's tricky).By default, Plotly Express sets
layout.heightto 600 but this can now also be overridden by settingpx.defaults.height(and setting that toNoneshould cause it to use the underlyingtemplate's height in principle although I haven't tested it).Batalex commentedon May 7, 2019
Wow, thanks for the quick response 🥇
Even though reading the colorscale directly from the template would perfectly fit my needs (I have a bunch of JSON serialized
Figureto convert to a custom template directly in a Dash app), I will give it a try.Closing for now, but I might reopen this issue.
nicolaskruchten commentedon May 7, 2019
Let's leave it open, that way I don't forget about this. I might be able to do something quickly :)
nicolaskruchten commentedon May 7, 2019
@jonmmease I'd love your input here...
I recently shuffled things up such that the defaults are no longer passed in from outside the core, but are set internally if the arguments are
None: 9726dd5Right now the default defaults are hardcoded at the top of
_core.pybut... I could just set those toNoneand add yet another layer of defaulting that would basically do "ifNonethen try to look inside the current setting oftemplateto grab some colors/heights". (and I would leave the default template toplotlyso this would mean we'd still use the right colors, other than falling back to Plotly rather than the Plasma I hardcoded today 🙈 )Thoughts?
nicolaskruchten commentedon May 7, 2019
@wbrgss this would mean you could use templates for DDK :)
wbrgss commentedon May 7, 2019
That makes sense to me @nicolaskruchten. We should also double-check
It would be nice if there was a logical correspondence between the
pxtemplate and a user-specified template, so anypxdefault parameter set toNonehas a template fallback.I wonder which parameters (here, right?) wouldn't play well with
templatedefaults. Do you forsee any problems withcolor_discrete_map(even though it's not a default)? That's gonna overridecolor_discrete_sequenceno matter what, right?Which places besides
layout.template? I'm guessing you'd have to crawl for other template parameters?nicolaskruchten commentedon May 8, 2019
The
pxdefaults are very few: color scale and sequence, symbol/dash sequences and dimensions. Everything else is use-specified. Among those, color scale&sequence and width/height are the only ones that are present in the template.Actually "[the template]" is not what I meant, I meant the colors :) In a template, we have color scales and sequences in a few places in
layoutsuch aslayout.colorway,layout.piecolorway,layout.sunburstcolorway,layout.colorscale.sequential,layout.colorscale.sequentialminus,layout.colorscale.divergingbut also on a per-trace-type basis: a template can specify one colorscale for scatter and another for heatmaps, and it can also specify that the first scatter is one color and the second is another, at which point I think that scatter traces will end up alternating between the two colors. So... yeah, there are a lot of ways to specify colors in a template.All that said,
pxcould just ignore most of that complexity and try to read fromtemplate.layout.colorwayandtemplate.layout.colorscale.sequentialand call it a day :)nicolaskruchten commentedon May 9, 2019
@Batalex if you want to take a look at or play with #74 I'd be curious to know what you think.
I played with the
heightthing and no matter what I do, there's no way I can getheightfromtemplateto impact the resulting figure, so I suspect there's something in the way we display in a notebook which prevents this from happening but I'm not sure what. As a result #74 leaves the default 600 in and you'd have to set the default toNoneto let the template shine through.nicolaskruchten commentedon May 10, 2019
@Batalex this is now released! You should be able to set
pio.templates.defaultandplotly_expresswill grab the colors from there, and it also respectspx.defaults.templateas well as thetemplatekwarg.Batalex commentedon May 13, 2019
@nicolaskruchten I apologize for the poor responsiveness. Many thanks, it works like a charm 👍
nicolaskruchten commentedon May 13, 2019
Super!