Skip to content

adding Turbo colorscale and continuous swatches #2882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- `go.Figure` now has a `set_subplots` method to set subplots on an already
existing figure.
- Added `Turbo` colorscale


## [4.12.1] - UNRELEASED
Expand Down
14 changes: 7 additions & 7 deletions doc/python/builtin-colorscales.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.2'
jupytext_version: 1.3.1
jupytext_version: 1.4.2
kernelspec:
display_name: Python 3
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.8
version: 3.7.7
plotly:
description: A reference for the built-in named continuous (sequential, diverging
and cylclical) color scales in Plotly.
Expand Down Expand Up @@ -83,11 +83,11 @@ Here are all the built-in scales in the `plotly.colors.sequential` module:
```python
import plotly.express as px

fig = px.colors.sequential.swatches()
fig = px.colors.sequential.swatches_continuous()
fig.show()
```

Note: `RdBu` was included in this module by mistake, even though it is a diverging color scale.
Note: `RdBu` was included in the `sequential` module by mistake, even though it is a diverging color scale.
It is intentionally left in for backwards-compatibility reasons.

### Built-In Diverging Color scales
Expand All @@ -102,7 +102,7 @@ Here are all the built-in scales in the `plotly.colors.diverging` module:
```python
import plotly.express as px

fig = px.colors.diverging.swatches().update_layout(margin_b=10)
fig = px.colors.diverging.swatches_continuous()
fig.show()
```

Expand All @@ -121,6 +121,6 @@ import plotly.express as px
fig = px.colors.cyclical.swatches_cyclical()
fig.show()

fig = px.colors.cyclical.swatches()
fig = px.colors.cyclical.swatches_continuous()
fig.show()
```
```
112 changes: 112 additions & 0 deletions packages/python/plotly/_plotly_utils/colors/_swatches.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,115 @@ def _swatches(module_names, module_contents, template=None):
margin=dict(b=10),
),
)


def _swatches_continuous(module_names, module_contents, template=None):
"""
Parameters
----------
template : str or dict or plotly.graph_objects.layout.Template instance
The figure template name or definition.

Returns
-------
fig : graph_objects.Figure containing the displayed image
A `Figure` object. This figure demonstrates the color scales and
sequences in this module, as stacked bar charts.
"""
import plotly.graph_objs as go
from plotly.express._core import apply_default_cascade

args = dict(template=template)
apply_default_cascade(args)

sequences = [
(k, v)
for k, v in module_contents.items()
if not (k.startswith("_") or k.startswith("swatches") or k.endswith("_r"))
]

n = 100

return go.Figure(
data=[
go.Bar(
orientation="h",
y=[name] * n,
x=[1] * n,
customdata=[(x + 1) / n for x in range(n)],
marker=dict(color=list(range(n)), colorscale=name, line_width=0),
hovertemplate="%{customdata}",
name=name,
)
for name, colors in reversed(sequences)
],
layout=dict(
title="plotly.colors." + module_names.split(".")[-1],
barmode="stack",
barnorm="fraction",
bargap=0.3,
showlegend=False,
xaxis=dict(range=[-0.02, 1.02], showticklabels=False, showgrid=False),
height=max(600, 40 * len(sequences)),
width=500,
template=args["template"],
margin=dict(b=10),
),
)


def _swatches_cyclical(module_names, module_contents, template=None):
"""
Parameters
----------
template : str or dict or plotly.graph_objects.layout.Template instance
The figure template name or definition.

Returns
-------
fig : graph_objects.Figure containing the displayed image
A `Figure` object. This figure demonstrates the color scales and
sequences in this module, as polar bar charts.
"""
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly.express._core import apply_default_cascade

args = dict(template=template)
apply_default_cascade(args)

rows = 2
cols = 4
scales = [
(k, v)
for k, v in module_contents.items()
if not (k.startswith("_") or k.startswith("swatches") or k.endswith("_r"))
]
names = [name for name, colors in scales]
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=names,
specs=[[{"type": "polar"}] * cols] * rows,
)

for i, (name, scale) in enumerate(scales):
fig.add_trace(
go.Barpolar(
r=[1] * int(360 / 5),
theta=list(range(0, 360, 5)),
marker_color=list(range(0, 360, 5)),
marker_cmin=0,
marker_cmax=360,
marker_colorscale=name,
name=name,
),
row=int(i / cols) + 1,
col=i % cols + 1,
)
fig.update_traces(width=5.2, marker_line_width=0, base=0.5, showlegend=False)
fig.update_polars(angularaxis_visible=False, radialaxis_visible=False)
fig.update_layout(
title="plotly.colors." + module_names.split(".")[-1], template=args["template"]
)
return fig
2 changes: 1 addition & 1 deletion packages/python/plotly/_plotly_utils/colors/carto.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,6 @@ def swatches(template=None):
# Prefix variable names with _ so that they will not be added to the swatches
_contents = dict(globals())
for _k, _cols in _contents.items():
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
continue
globals()[_k + "_r"] = _cols[::-1]
12 changes: 10 additions & 2 deletions packages/python/plotly/_plotly_utils/colors/cmocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
cmocean is made available under an MIT license: https://github.com/matplotlib/cmocean/blob/master/LICENSE.txt
"""

from ._swatches import _swatches
from ._swatches import _swatches, _swatches_continuous


def swatches(template=None):
Expand All @@ -15,6 +15,14 @@ def swatches(template=None):

swatches.__doc__ = _swatches.__doc__


def swatches_continuous(template=None):
return _swatches_continuous(__name__, globals(), template)


swatches_continuous.__doc__ = _swatches_continuous.__doc__


turbid = [
"rgb(232, 245, 171)",
"rgb(220, 219, 137)",
Expand Down Expand Up @@ -271,6 +279,6 @@ def swatches(template=None):
# Prefix variable names with _ so that they will not be added to the swatches
_contents = dict(globals())
for _k, _cols in _contents.items():
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
continue
globals()[_k + "_r"] = _cols[::-1]
2 changes: 1 addition & 1 deletion packages/python/plotly/_plotly_utils/colors/colorbrewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,6 @@ def swatches(template=None):
# Prefix variable names with _ so that they will not be added to the swatches
_contents = dict(globals())
for _k, _cols in _contents.items():
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
continue
globals()[_k + "_r"] = _cols[::-1]
60 changes: 12 additions & 48 deletions packages/python/plotly/_plotly_utils/colors/cyclical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
complex numbers or other phase data.
"""

from ._swatches import _swatches
from ._swatches import _swatches, _swatches_continuous, _swatches_cyclical


def swatches(template=None):
Expand All @@ -14,54 +14,18 @@ def swatches(template=None):
swatches.__doc__ = _swatches.__doc__


def swatches_continuous(template=None):
return _swatches_continuous(__name__, globals(), template)


swatches_continuous.__doc__ = _swatches_continuous.__doc__


def swatches_cyclical(template=None):
"""
Parameters
----------
template : str or dict or plotly.graph_objects.layout.Template instance
The figure template name or definition.

Returns
-------
fig : graph_objects.Figure containing the displayed image
A `Figure` object. This figure demonstrates the color scales and
sequences in this module, as polar bar charts.
"""
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly.express._core import apply_default_cascade

args = dict(template=template)
apply_default_cascade(args)

rows = 2
cols = 4
scales = ["Twilight", "IceFire", "Edge", "Phase", "HSV", "mrybm", "mygbm"]
fig = make_subplots(
rows=rows,
cols=cols,
subplot_titles=scales,
specs=[[{"type": "polar"}] * cols] * rows,
)

for i, scale in enumerate(scales):
fig.add_trace(
go.Barpolar(
r=[1] * int(360 / 5),
theta=list(range(0, 360, 5)),
marker_color=list(range(0, 360, 5)),
marker_cmin=0,
marker_cmax=360,
marker_colorscale=scale,
name=scale,
),
row=int(i / cols) + 1,
col=i % cols + 1,
)
fig.update_traces(width=5.2, marker_line_width=0, base=0.5, showlegend=False)
fig.update_polars(angularaxis_visible=False, radialaxis_visible=False)
fig.update_layout(title="plotly.colors.cyclical", template=args["template"])
return fig
return _swatches_cyclical(__name__, globals(), template)


swatches_cyclical.__doc__ = _swatches_cyclical.__doc__


Twilight = [
Expand Down
13 changes: 10 additions & 3 deletions packages/python/plotly/_plotly_utils/colors/diverging.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
RdYlGn,
Spectral,
)
from .cmocean import balance, delta, curl # noqa: F401
from .cmocean import balance, delta, curl, oxy # noqa: F401
from .carto import Armyrose, Fall, Geyser, Temps, Tealrose, Tropic, Earth # noqa: F401

from .plotlyjs import Picnic, Portland # noqa: F401

from ._swatches import _swatches
from ._swatches import _swatches, _swatches_continuous


def swatches(template=None):
Expand All @@ -31,10 +31,17 @@ def swatches(template=None):

swatches.__doc__ = _swatches.__doc__


def swatches_continuous(template=None):
return _swatches_continuous(__name__, globals(), template)


swatches_continuous.__doc__ = _swatches_continuous.__doc__

# Prefix variable names with _ so that they will not be added to the swatches
_contents = dict(globals())
for _k, _cols in _contents.items():
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
continue
globals()[_k + "_r"] = _cols[::-1]

Expand Down
2 changes: 1 addition & 1 deletion packages/python/plotly/_plotly_utils/colors/plotlyjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@
# Prefix variable names with _ so that they will not be added to the swatches
_contents = dict(globals())
for _k, _cols in _contents.items():
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
continue
globals()[_k + "_r"] = _cols[::-1]
2 changes: 1 addition & 1 deletion packages/python/plotly/_plotly_utils/colors/qualitative.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def swatches(template=None):
# Prefix variable names with _ so that they will not be added to the swatches
_contents = dict(globals())
for _k, _cols in _contents.items():
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
continue
globals()[_k + "_r"] = _cols[::-1]

Expand Down
Loading