Skip to content

Commit b6e5636

Browse files
Merge pull request #3186 from CarlAndersson/get-colorscale-by-name
Implements getting a colorscale by name
2 parents fa292ed + b597748 commit b6e5636

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -806,3 +806,30 @@ def named_colorscales():
806806
from _plotly_utils.basevalidators import ColorscaleValidator
807807

808808
return [c for c in ColorscaleValidator("", "").named_colorscales]
809+
810+
811+
def get_colorscale(name):
812+
"""
813+
Returns the colorscale for a given name. See `named_colorscales` for the
814+
built-in colorscales.
815+
"""
816+
from _plotly_utils.basevalidators import ColorscaleValidator
817+
818+
if not isinstance(name, str):
819+
raise exceptions.PlotlyError("Name argument have to be a string.")
820+
821+
name = name.lower()
822+
if name[-2:] == "_r":
823+
should_reverse = True
824+
name = name[:-2]
825+
else:
826+
should_reverse = False
827+
828+
if name in ColorscaleValidator("", "").named_colorscales:
829+
colorscale = ColorscaleValidator("", "").named_colorscales[name]
830+
else:
831+
raise exceptions.PlotlyError(f"Colorscale {name} is not a built-in scale.")
832+
833+
if should_reverse:
834+
return colorscale[::-1]
835+
return colorscale

Diff for: packages/python/plotly/plotly/colors/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@
4545
"plotlyjs",
4646
"DEFAULT_PLOTLY_COLORS",
4747
"PLOTLY_SCALES",
48+
"get_colorscale",
4849
]

Diff for: packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py

+27
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,30 @@ def test_make_colorscale(self):
137137
self.assertRaisesRegexp(
138138
PlotlyError, pattern2, colors.make_colorscale, color_list2, scale
139139
)
140+
141+
def test_get_colorscale(self):
142+
143+
# test for incorrect input type
144+
pattern = "Name argument have to be a string."
145+
name = colors.sequential.haline
146+
147+
self.assertRaisesRegexp(PlotlyError, pattern, colors.get_colorscale, name)
148+
149+
# test for non-existing colorscale
150+
pattern = r"Colorscale \S+ is not a built-in scale."
151+
name = "foo"
152+
153+
self.assertRaisesRegex(PlotlyError, pattern, colors.get_colorscale, name)
154+
155+
# test non-capitalised access
156+
self.assertEqual(colors.sequential.haline, colors.get_colorscale("haline"))
157+
# test capitalised access
158+
self.assertEqual(colors.diverging.Earth, colors.get_colorscale("Earth"))
159+
# test accessing non-capitalised scale with capitalised name
160+
self.assertEqual(colors.cyclical.mrybm, colors.get_colorscale("Mrybm"))
161+
# test accessing capitalised scale with non-capitalised name
162+
self.assertEqual(colors.sequential.Viridis, colors.get_colorscale("viridis"))
163+
# test accessing reversed scale
164+
self.assertEqual(
165+
colors.diverging.Portland_r, colors.get_colorscale("portland_r")
166+
)

0 commit comments

Comments
 (0)