Skip to content

Commit 259fe11

Browse files
Merge pull request #2882 from plotly/new_colorscales
adding Turbo colorscale and continuous swatches
2 parents a3550e6 + 5c3ecdd commit 259fe11

File tree

11 files changed

+182
-67
lines changed

11 files changed

+182
-67
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
88

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

1213

1314
## [4.12.1] - UNRELEASED

Diff for: doc/python/builtin-colorscales.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.3.1
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.8
23+
version: 3.7.7
2424
plotly:
2525
description: A reference for the built-in named continuous (sequential, diverging
2626
and cylclical) color scales in Plotly.
@@ -83,11 +83,11 @@ Here are all the built-in scales in the `plotly.colors.sequential` module:
8383
```python
8484
import plotly.express as px
8585

86-
fig = px.colors.sequential.swatches()
86+
fig = px.colors.sequential.swatches_continuous()
8787
fig.show()
8888
```
8989

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

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

105-
fig = px.colors.diverging.swatches().update_layout(margin_b=10)
105+
fig = px.colors.diverging.swatches_continuous()
106106
fig.show()
107107
```
108108

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

124-
fig = px.colors.cyclical.swatches()
124+
fig = px.colors.cyclical.swatches_continuous()
125125
fig.show()
126-
```
126+
```

Diff for: packages/python/plotly/_plotly_utils/colors/_swatches.py

+112
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,115 @@ def _swatches(module_names, module_contents, template=None):
4747
margin=dict(b=10),
4848
),
4949
)
50+
51+
52+
def _swatches_continuous(module_names, module_contents, template=None):
53+
"""
54+
Parameters
55+
----------
56+
template : str or dict or plotly.graph_objects.layout.Template instance
57+
The figure template name or definition.
58+
59+
Returns
60+
-------
61+
fig : graph_objects.Figure containing the displayed image
62+
A `Figure` object. This figure demonstrates the color scales and
63+
sequences in this module, as stacked bar charts.
64+
"""
65+
import plotly.graph_objs as go
66+
from plotly.express._core import apply_default_cascade
67+
68+
args = dict(template=template)
69+
apply_default_cascade(args)
70+
71+
sequences = [
72+
(k, v)
73+
for k, v in module_contents.items()
74+
if not (k.startswith("_") or k.startswith("swatches") or k.endswith("_r"))
75+
]
76+
77+
n = 100
78+
79+
return go.Figure(
80+
data=[
81+
go.Bar(
82+
orientation="h",
83+
y=[name] * n,
84+
x=[1] * n,
85+
customdata=[(x + 1) / n for x in range(n)],
86+
marker=dict(color=list(range(n)), colorscale=name, line_width=0),
87+
hovertemplate="%{customdata}",
88+
name=name,
89+
)
90+
for name, colors in reversed(sequences)
91+
],
92+
layout=dict(
93+
title="plotly.colors." + module_names.split(".")[-1],
94+
barmode="stack",
95+
barnorm="fraction",
96+
bargap=0.3,
97+
showlegend=False,
98+
xaxis=dict(range=[-0.02, 1.02], showticklabels=False, showgrid=False),
99+
height=max(600, 40 * len(sequences)),
100+
width=500,
101+
template=args["template"],
102+
margin=dict(b=10),
103+
),
104+
)
105+
106+
107+
def _swatches_cyclical(module_names, module_contents, template=None):
108+
"""
109+
Parameters
110+
----------
111+
template : str or dict or plotly.graph_objects.layout.Template instance
112+
The figure template name or definition.
113+
114+
Returns
115+
-------
116+
fig : graph_objects.Figure containing the displayed image
117+
A `Figure` object. This figure demonstrates the color scales and
118+
sequences in this module, as polar bar charts.
119+
"""
120+
import plotly.graph_objects as go
121+
from plotly.subplots import make_subplots
122+
from plotly.express._core import apply_default_cascade
123+
124+
args = dict(template=template)
125+
apply_default_cascade(args)
126+
127+
rows = 2
128+
cols = 4
129+
scales = [
130+
(k, v)
131+
for k, v in module_contents.items()
132+
if not (k.startswith("_") or k.startswith("swatches") or k.endswith("_r"))
133+
]
134+
names = [name for name, colors in scales]
135+
fig = make_subplots(
136+
rows=rows,
137+
cols=cols,
138+
subplot_titles=names,
139+
specs=[[{"type": "polar"}] * cols] * rows,
140+
)
141+
142+
for i, (name, scale) in enumerate(scales):
143+
fig.add_trace(
144+
go.Barpolar(
145+
r=[1] * int(360 / 5),
146+
theta=list(range(0, 360, 5)),
147+
marker_color=list(range(0, 360, 5)),
148+
marker_cmin=0,
149+
marker_cmax=360,
150+
marker_colorscale=name,
151+
name=name,
152+
),
153+
row=int(i / cols) + 1,
154+
col=i % cols + 1,
155+
)
156+
fig.update_traces(width=5.2, marker_line_width=0, base=0.5, showlegend=False)
157+
fig.update_polars(angularaxis_visible=False, radialaxis_visible=False)
158+
fig.update_layout(
159+
title="plotly.colors." + module_names.split(".")[-1], template=args["template"]
160+
)
161+
return fig

Diff for: packages/python/plotly/_plotly_utils/colors/carto.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,6 @@ def swatches(template=None):
386386
# Prefix variable names with _ so that they will not be added to the swatches
387387
_contents = dict(globals())
388388
for _k, _cols in _contents.items():
389-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
389+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
390390
continue
391391
globals()[_k + "_r"] = _cols[::-1]

Diff for: packages/python/plotly/_plotly_utils/colors/cmocean.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
cmocean is made available under an MIT license: https://github.com/matplotlib/cmocean/blob/master/LICENSE.txt
77
"""
88

9-
from ._swatches import _swatches
9+
from ._swatches import _swatches, _swatches_continuous
1010

1111

1212
def swatches(template=None):
@@ -15,6 +15,14 @@ def swatches(template=None):
1515

1616
swatches.__doc__ = _swatches.__doc__
1717

18+
19+
def swatches_continuous(template=None):
20+
return _swatches_continuous(__name__, globals(), template)
21+
22+
23+
swatches_continuous.__doc__ = _swatches_continuous.__doc__
24+
25+
1826
turbid = [
1927
"rgb(232, 245, 171)",
2028
"rgb(220, 219, 137)",
@@ -271,6 +279,6 @@ def swatches(template=None):
271279
# Prefix variable names with _ so that they will not be added to the swatches
272280
_contents = dict(globals())
273281
for _k, _cols in _contents.items():
274-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
282+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
275283
continue
276284
globals()[_k + "_r"] = _cols[::-1]

Diff for: packages/python/plotly/_plotly_utils/colors/colorbrewer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,6 @@ def swatches(template=None):
460460
# Prefix variable names with _ so that they will not be added to the swatches
461461
_contents = dict(globals())
462462
for _k, _cols in _contents.items():
463-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
463+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
464464
continue
465465
globals()[_k + "_r"] = _cols[::-1]

Diff for: packages/python/plotly/_plotly_utils/colors/cyclical.py

+12-48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
complex numbers or other phase data.
55
"""
66

7-
from ._swatches import _swatches
7+
from ._swatches import _swatches, _swatches_continuous, _swatches_cyclical
88

99

1010
def swatches(template=None):
@@ -14,54 +14,18 @@ def swatches(template=None):
1414
swatches.__doc__ = _swatches.__doc__
1515

1616

17+
def swatches_continuous(template=None):
18+
return _swatches_continuous(__name__, globals(), template)
19+
20+
21+
swatches_continuous.__doc__ = _swatches_continuous.__doc__
22+
23+
1724
def swatches_cyclical(template=None):
18-
"""
19-
Parameters
20-
----------
21-
template : str or dict or plotly.graph_objects.layout.Template instance
22-
The figure template name or definition.
23-
24-
Returns
25-
-------
26-
fig : graph_objects.Figure containing the displayed image
27-
A `Figure` object. This figure demonstrates the color scales and
28-
sequences in this module, as polar bar charts.
29-
"""
30-
import plotly.graph_objects as go
31-
from plotly.subplots import make_subplots
32-
from plotly.express._core import apply_default_cascade
33-
34-
args = dict(template=template)
35-
apply_default_cascade(args)
36-
37-
rows = 2
38-
cols = 4
39-
scales = ["Twilight", "IceFire", "Edge", "Phase", "HSV", "mrybm", "mygbm"]
40-
fig = make_subplots(
41-
rows=rows,
42-
cols=cols,
43-
subplot_titles=scales,
44-
specs=[[{"type": "polar"}] * cols] * rows,
45-
)
46-
47-
for i, scale in enumerate(scales):
48-
fig.add_trace(
49-
go.Barpolar(
50-
r=[1] * int(360 / 5),
51-
theta=list(range(0, 360, 5)),
52-
marker_color=list(range(0, 360, 5)),
53-
marker_cmin=0,
54-
marker_cmax=360,
55-
marker_colorscale=scale,
56-
name=scale,
57-
),
58-
row=int(i / cols) + 1,
59-
col=i % cols + 1,
60-
)
61-
fig.update_traces(width=5.2, marker_line_width=0, base=0.5, showlegend=False)
62-
fig.update_polars(angularaxis_visible=False, radialaxis_visible=False)
63-
fig.update_layout(title="plotly.colors.cyclical", template=args["template"])
64-
return fig
25+
return _swatches_cyclical(__name__, globals(), template)
26+
27+
28+
swatches_cyclical.__doc__ = _swatches_cyclical.__doc__
6529

6630

6731
Twilight = [

Diff for: packages/python/plotly/_plotly_utils/colors/diverging.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
RdYlGn,
1818
Spectral,
1919
)
20-
from .cmocean import balance, delta, curl # noqa: F401
20+
from .cmocean import balance, delta, curl, oxy # noqa: F401
2121
from .carto import Armyrose, Fall, Geyser, Temps, Tealrose, Tropic, Earth # noqa: F401
2222

2323
from .plotlyjs import Picnic, Portland # noqa: F401
2424

25-
from ._swatches import _swatches
25+
from ._swatches import _swatches, _swatches_continuous
2626

2727

2828
def swatches(template=None):
@@ -31,10 +31,17 @@ def swatches(template=None):
3131

3232
swatches.__doc__ = _swatches.__doc__
3333

34+
35+
def swatches_continuous(template=None):
36+
return _swatches_continuous(__name__, globals(), template)
37+
38+
39+
swatches_continuous.__doc__ = _swatches_continuous.__doc__
40+
3441
# Prefix variable names with _ so that they will not be added to the swatches
3542
_contents = dict(globals())
3643
for _k, _cols in _contents.items():
37-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
44+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
3845
continue
3946
globals()[_k + "_r"] = _cols[::-1]
4047

Diff for: packages/python/plotly/_plotly_utils/colors/plotlyjs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,6 @@
183183
# Prefix variable names with _ so that they will not be added to the swatches
184184
_contents = dict(globals())
185185
for _k, _cols in _contents.items():
186-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
186+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
187187
continue
188188
globals()[_k + "_r"] = _cols[::-1]

Diff for: packages/python/plotly/_plotly_utils/colors/qualitative.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def swatches(template=None):
149149
# Prefix variable names with _ so that they will not be added to the swatches
150150
_contents = dict(globals())
151151
for _k, _cols in _contents.items():
152-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
152+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
153153
continue
154154
globals()[_k + "_r"] = _cols[::-1]
155155

0 commit comments

Comments
 (0)