Skip to content

Commit d4f2d59

Browse files
adding Turbo colorscale and continuous swatches
1 parent a3550e6 commit d4f2d59

File tree

10 files changed

+179
-67
lines changed

10 files changed

+179
-67
lines changed

doc/python/builtin-colorscales.md

Lines changed: 7 additions & 7 deletions
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+
```

packages/python/plotly/_plotly_utils/colors/_swatches.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,113 @@ 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+
marker=dict(color=list(range(n)), colorscale=name, line_width=0),
86+
)
87+
for name, colors in reversed(sequences)
88+
],
89+
layout=dict(
90+
title="plotly.colors." + module_names.split(".")[-1],
91+
barmode="stack",
92+
barnorm="fraction",
93+
bargap=0.3,
94+
showlegend=False,
95+
xaxis=dict(range=[-0.02, 1.02], showticklabels=False, showgrid=False),
96+
height=max(600, 40 * len(sequences)),
97+
width=500,
98+
template=args["template"],
99+
margin=dict(b=10),
100+
),
101+
)
102+
103+
104+
def _swatches_cyclical(module_names, module_contents, template=None):
105+
"""
106+
Parameters
107+
----------
108+
template : str or dict or plotly.graph_objects.layout.Template instance
109+
The figure template name or definition.
110+
111+
Returns
112+
-------
113+
fig : graph_objects.Figure containing the displayed image
114+
A `Figure` object. This figure demonstrates the color scales and
115+
sequences in this module, as polar bar charts.
116+
"""
117+
import plotly.graph_objects as go
118+
from plotly.subplots import make_subplots
119+
from plotly.express._core import apply_default_cascade
120+
121+
args = dict(template=template)
122+
apply_default_cascade(args)
123+
124+
rows = 2
125+
cols = 4
126+
scales = [
127+
(k, v)
128+
for k, v in module_contents.items()
129+
if not (k.startswith("_") or k.startswith("swatches") or k.endswith("_r"))
130+
]
131+
names = [name for name, colors in scales]
132+
fig = make_subplots(
133+
rows=rows,
134+
cols=cols,
135+
subplot_titles=names,
136+
specs=[[{"type": "polar"}] * cols] * rows,
137+
)
138+
139+
for i, (name, scale) in enumerate(scales):
140+
fig.add_trace(
141+
go.Barpolar(
142+
r=[1] * int(360 / 5),
143+
theta=list(range(0, 360, 5)),
144+
marker_color=list(range(0, 360, 5)),
145+
marker_cmin=0,
146+
marker_cmax=360,
147+
marker_colorscale=name,
148+
name=name,
149+
),
150+
row=int(i / cols) + 1,
151+
col=i % cols + 1,
152+
)
153+
fig.update_traces(width=5.2, marker_line_width=0, base=0.5, showlegend=False)
154+
fig.update_polars(angularaxis_visible=False, radialaxis_visible=False)
155+
fig.update_layout(
156+
title="plotly.colors." + module_names.split(".")[-1], template=args["template"]
157+
)
158+
return fig
159+

packages/python/plotly/_plotly_utils/colors/carto.py

Lines changed: 1 addition & 1 deletion
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]

packages/python/plotly/_plotly_utils/colors/cmocean.py

Lines changed: 10 additions & 2 deletions
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]

packages/python/plotly/_plotly_utils/colors/colorbrewer.py

Lines changed: 1 addition & 1 deletion
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]

packages/python/plotly/_plotly_utils/colors/cyclical.py

Lines changed: 12 additions & 48 deletions
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 = [

packages/python/plotly/_plotly_utils/colors/diverging.py

Lines changed: 10 additions & 3 deletions
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

packages/python/plotly/_plotly_utils/colors/plotlyjs.py

Lines changed: 1 addition & 1 deletion
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]

packages/python/plotly/_plotly_utils/colors/qualitative.py

Lines changed: 1 addition & 1 deletion
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

packages/python/plotly/_plotly_utils/colors/sequential.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
mostly meant to be passed in as the `color_continuous_scale` argument to various functions.
66
"""
77

8-
from ._swatches import _swatches
8+
from ._swatches import _swatches, _swatches_continuous
99

1010

1111
def swatches(template=None):
@@ -14,6 +14,13 @@ def swatches(template=None):
1414

1515
swatches.__doc__ = _swatches.__doc__
1616

17+
18+
def swatches_continuous(template=None):
19+
return _swatches_continuous(__name__, globals(), template)
20+
21+
22+
swatches_continuous.__doc__ = _swatches_continuous.__doc__
23+
1724
Plotly3 = [
1825
"#0508b8",
1926
"#1910d8",
@@ -91,7 +98,23 @@ def swatches(template=None):
9198
"#fdca26",
9299
"#f0f921",
93100
]
94-
101+
Turbo = [
102+
"#30123b",
103+
"#4145ab",
104+
"#4675ed",
105+
"#39a2fc",
106+
"#1bcfd4",
107+
"#24eca6",
108+
"#61fc6c",
109+
"#a4fc3b",
110+
"#d1e834",
111+
"#f3c63a",
112+
"#fe9b2d",
113+
"#f36315",
114+
"#d93806",
115+
"#b11901",
116+
"#7a0402",
117+
]
95118
from .plotlyjs import Blackbody, Bluered, Electric, Hot, Jet, Rainbow # noqa: F401
96119

97120
from .colorbrewer import ( # noqa: F401
@@ -159,7 +182,7 @@ def swatches(template=None):
159182
# Prefix variable names with _ so that they will not be added to the swatches
160183
_contents = dict(globals())
161184
for _k, _cols in _contents.items():
162-
if _k.startswith("_") or _k == "swatches" or _k.endswith("_r"):
185+
if _k.startswith("_") or _k.startswith("swatches") or _k.endswith("_r"):
163186
continue
164187
globals()[_k + "_r"] = _cols[::-1]
165188

0 commit comments

Comments
 (0)