Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Plotly Express does not play well with templates #70

Closed
Batalex opened this issue May 7, 2019 · 11 comments · Fixed by #74
Closed

Plotly Express does not play well with templates #70

Batalex opened this issue May 7, 2019 · 11 comments · Fixed by #74

Comments

@Batalex
Copy link

Batalex commented May 7, 2019

Hi,

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?

@nicolaskruchten
Copy link
Contributor

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).

@Batalex
Copy link
Author

Batalex commented 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 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.

@Batalex Batalex closed this as completed May 7, 2019
@nicolaskruchten
Copy link
Contributor

Let's leave it open, that way I don't forget about this. I might be able to do something quickly :)

@nicolaskruchten
Copy link
Contributor

@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: 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 🙈 )

Thoughts?

@nicolaskruchten
Copy link
Contributor

@wbrgss this would mean you could use templates for DDK :)

@wbrgss
Copy link

wbrgss commented May 7, 2019

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?

@nicolaskruchten
Copy link
Contributor

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.diverging but 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 :)

@nicolaskruchten
Copy link
Contributor

@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.

@nicolaskruchten
Copy link
Contributor

@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.

@Batalex
Copy link
Author

Batalex commented May 13, 2019

@nicolaskruchten I apologize for the poor responsiveness. Many thanks, it works like a charm 👍

@nicolaskruchten
Copy link
Contributor

Super!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants