From c207edbcc8cff4a21e28b184c7a998fe1d0eff63 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Fri, 6 May 2022 14:28:37 -0400 Subject: [PATCH 1/2] stop fiddling with exec and globals --- .../plotly/_plotly_utils/colors/carto.py | 40 +- .../plotly/_plotly_utils/colors/cmocean.py | 24 +- .../_plotly_utils/colors/colorbrewer.py | 41 +- .../plotly/_plotly_utils/colors/cyclical.py | 14 +- .../plotly/_plotly_utils/colors/diverging.py | 46 ++- .../plotly/_plotly_utils/colors/plotlyjs.py | 354 +++++++++--------- .../_plotly_utils/colors/qualitative.py | 43 ++- .../plotly/_plotly_utils/colors/sequential.py | 85 ++++- .../test_optional/test_px/test_colors.py | 29 ++ 9 files changed, 449 insertions(+), 227 deletions(-) diff --git a/packages/python/plotly/_plotly_utils/colors/carto.py b/packages/python/plotly/_plotly_utils/colors/carto.py index 919d7c99aa5..f5c503f3251 100644 --- a/packages/python/plotly/_plotly_utils/colors/carto.py +++ b/packages/python/plotly/_plotly_utils/colors/carto.py @@ -383,9 +383,37 @@ def swatches(template=None): "rgb(165, 170, 153)", ] -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] +Aggrnyl_r = Aggrnyl[::-1] +Agsunset_r = Agsunset[::-1] +Antique_r = Antique[::-1] +Armyrose_r = Armyrose[::-1] +Blugrn_r = Blugrn[::-1] +Bluyl_r = Bluyl[::-1] +Bold_r = Bold[::-1] +Brwnyl_r = Brwnyl[::-1] +Burg_r = Burg[::-1] +Burgyl_r = Burgyl[::-1] +Darkmint_r = Darkmint[::-1] +Earth_r = Earth[::-1] +Emrld_r = Emrld[::-1] +Fall_r = Fall[::-1] +Geyser_r = Geyser[::-1] +Magenta_r = Magenta[::-1] +Mint_r = Mint[::-1] +Oryel_r = Oryel[::-1] +Pastel_r = Pastel[::-1] +Peach_r = Peach[::-1] +Pinkyl_r = Pinkyl[::-1] +Prism_r = Prism[::-1] +Purp_r = Purp[::-1] +Purpor_r = Purpor[::-1] +Redor_r = Redor[::-1] +Safe_r = Safe[::-1] +Sunset_r = Sunset[::-1] +Sunsetdark_r = Sunsetdark[::-1] +Teal_r = Teal[::-1] +Tealgrn_r = Tealgrn[::-1] +Tealrose_r = Tealrose[::-1] +Temps_r = Temps[::-1] +Tropic_r = Tropic[::-1] +Vivid_r = Vivid[::-1] diff --git a/packages/python/plotly/_plotly_utils/colors/cmocean.py b/packages/python/plotly/_plotly_utils/colors/cmocean.py index d5788712bb9..18944b59361 100644 --- a/packages/python/plotly/_plotly_utils/colors/cmocean.py +++ b/packages/python/plotly/_plotly_utils/colors/cmocean.py @@ -276,9 +276,21 @@ def swatches_continuous(template=None): "rgb(51, 13, 53)", ] -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] +algae_r = algae[::-1] +amp_r = amp[::-1] +balance_r = balance[::-1] +curl_r = curl[::-1] +deep_r = deep[::-1] +delta_r = delta[::-1] +dense_r = dense[::-1] +gray_r = gray[::-1] +haline_r = haline[::-1] +ice_r = ice[::-1] +matter_r = matter[::-1] +oxy_r = oxy[::-1] +phase_r = phase[::-1] +solar_r = solar[::-1] +speed_r = speed[::-1] +tempo_r = tempo[::-1] +thermal_r = thermal[::-1] +turbid_r = turbid[::-1] diff --git a/packages/python/plotly/_plotly_utils/colors/colorbrewer.py b/packages/python/plotly/_plotly_utils/colors/colorbrewer.py index 9e3098144b3..cbb286e43e0 100644 --- a/packages/python/plotly/_plotly_utils/colors/colorbrewer.py +++ b/packages/python/plotly/_plotly_utils/colors/colorbrewer.py @@ -457,9 +457,38 @@ def swatches(template=None): "rgb(128,0,38)", ] -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] +Accent_r = Accent[::-1] +Blues_r = Blues[::-1] +BrBG_r = BrBG[::-1] +BuGn_r = BuGn[::-1] +BuPu_r = BuPu[::-1] +Dark2_r = Dark2[::-1] +GnBu_r = GnBu[::-1] +Greens_r = Greens[::-1] +Greys_r = Greys[::-1] +OrRd_r = OrRd[::-1] +Oranges_r = Oranges[::-1] +PRGn_r = PRGn[::-1] +Paired_r = Paired[::-1] +Pastel1_r = Pastel1[::-1] +Pastel2_r = Pastel2[::-1] +PiYG_r = PiYG[::-1] +PuBu_r = PuBu[::-1] +PuBuGn_r = PuBuGn[::-1] +PuOr_r = PuOr[::-1] +PuRd_r = PuRd[::-1] +Purples_r = Purples[::-1] +RdBu_r = RdBu[::-1] +RdGy_r = RdGy[::-1] +RdPu_r = RdPu[::-1] +RdYlBu_r = RdYlBu[::-1] +RdYlGn_r = RdYlGn[::-1] +Reds_r = Reds[::-1] +Set1_r = Set1[::-1] +Set2_r = Set2[::-1] +Set3_r = Set3[::-1] +Spectral_r = Spectral[::-1] +YlGn_r = YlGn[::-1] +YlGnBu_r = YlGnBu[::-1] +YlOrBr_r = YlOrBr[::-1] +YlOrRd_r = YlOrRd[::-1] diff --git a/packages/python/plotly/_plotly_utils/colors/cyclical.py b/packages/python/plotly/_plotly_utils/colors/cyclical.py index 2cd1cead1a7..bcb7d07e2e3 100644 --- a/packages/python/plotly/_plotly_utils/colors/cyclical.py +++ b/packages/python/plotly/_plotly_utils/colors/cyclical.py @@ -143,13 +143,13 @@ def swatches_cyclical(template=None): "#ef55f1", ] -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] - +Edge_r = Edge[::-1] +HSV_r = HSV[::-1] +IceFire_r = IceFire[::-1] +Phase_r = Phase[::-1] +Twilight_r = Twilight[::-1] +mrybm_r = mrybm[::-1] +mygbm_r = mygbm[::-1] __all__ = [ "swatches", diff --git a/packages/python/plotly/_plotly_utils/colors/diverging.py b/packages/python/plotly/_plotly_utils/colors/diverging.py index 92d6d72f139..53170df9c09 100644 --- a/packages/python/plotly/_plotly_utils/colors/diverging.py +++ b/packages/python/plotly/_plotly_utils/colors/diverging.py @@ -16,11 +16,44 @@ RdYlBu, RdYlGn, Spectral, + BrBG_r, + PRGn_r, + PiYG_r, + PuOr_r, + RdBu_r, + RdGy_r, + RdYlBu_r, + RdYlGn_r, + Spectral_r, +) +from .cmocean import ( # noqa: F401 + balance, + delta, + curl, + oxy, + balance_r, + delta_r, + curl_r, + oxy_r, +) +from .carto import ( # noqa: F401 + Armyrose, + Fall, + Geyser, + Temps, + Tealrose, + Tropic, + Earth, + Armyrose_r, + Fall_r, + Geyser_r, + Temps_r, + Tealrose_r, + Tropic_r, + Earth_r, ) -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 .plotlyjs import Picnic, Portland, Picnic_r, Portland_r # noqa: F401 from ._swatches import _swatches, _swatches_continuous @@ -38,12 +71,5 @@ def swatches_continuous(template=None): 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] - __all__ = ["swatches"] diff --git a/packages/python/plotly/_plotly_utils/colors/plotlyjs.py b/packages/python/plotly/_plotly_utils/colors/plotlyjs.py index 0d8cd9944a4..3a0517e3c3c 100644 --- a/packages/python/plotly/_plotly_utils/colors/plotlyjs.py +++ b/packages/python/plotly/_plotly_utils/colors/plotlyjs.py @@ -1,30 +1,31 @@ # Copied from # https://github.com/plotly/plotly.js/blob/master/src/components/colorscale/scales.js -_plotlyjs_scales = { - "Greys": [[0, "rgb(0,0,0)"], [1, "rgb(255,255,255)"]], - "YlGnBu": [ - [0, "rgb(8,29,88)"], - [0.125, "rgb(37,52,148)"], - [0.25, "rgb(34,94,168)"], - [0.375, "rgb(29,145,192)"], - [0.5, "rgb(65,182,196)"], - [0.625, "rgb(127,205,187)"], - [0.75, "rgb(199,233,180)"], - [0.875, "rgb(237,248,217)"], - [1, "rgb(255,255,217)"], - ], - "Greens": [ - [0, "rgb(0,68,27)"], - [0.125, "rgb(0,109,44)"], - [0.25, "rgb(35,139,69)"], - [0.375, "rgb(65,171,93)"], - [0.5, "rgb(116,196,118)"], - [0.625, "rgb(161,217,155)"], - [0.75, "rgb(199,233,192)"], - [0.875, "rgb(229,245,224)"], - [1, "rgb(247,252,245)"], - ], - "YlOrRd": [ + +Greys = [[0, "rgb(0,0,0)"], [1, "rgb(255,255,255)"]] +YlGnBu = [ + [0, "rgb(8,29,88)"], + [0.125, "rgb(37,52,148)"], + [0.25, "rgb(34,94,168)"], + [0.375, "rgb(29,145,192)"], + [0.5, "rgb(65,182,196)"], + [0.625, "rgb(127,205,187)"], + [0.75, "rgb(199,233,180)"], + [0.875, "rgb(237,248,217)"], + [1, "rgb(255,255,217)"], +] +Greens = [ + [0, "rgb(0,68,27)"], + [0.125, "rgb(0,109,44)"], + [0.25, "rgb(35,139,69)"], + [0.375, "rgb(65,171,93)"], + [0.5, "rgb(116,196,118)"], + [0.625, "rgb(161,217,155)"], + [0.75, "rgb(199,233,192)"], + [0.875, "rgb(229,245,224)"], + [1, "rgb(247,252,245)"], +] +YlOrRd = ( + [ [0, "rgb(128,0,38)"], [0.125, "rgb(189,0,38)"], [0.25, "rgb(227,26,28)"], @@ -35,154 +36,157 @@ [0.875, "rgb(255,237,160)"], [1, "rgb(255,255,204)"], ], - "Bluered": [[0, "rgb(0,0,255)"], [1, "rgb(255,0,0)"]], - # modified RdBu based on - # http:#www.kennethmoreland.com/color-maps/ - "RdBu": [ - [0, "rgb(5,10,172)"], - [0.35, "rgb(106,137,247)"], - [0.5, "rgb(190,190,190)"], - [0.6, "rgb(220,170,132)"], - [0.7, "rgb(230,145,90)"], - [1, "rgb(178,10,28)"], - ], - # Scale for non-negative numeric values - "Reds": [ - [0, "rgb(220,220,220)"], - [0.2, "rgb(245,195,157)"], - [0.4, "rgb(245,160,105)"], - [1, "rgb(178,10,28)"], - ], - # Scale for non-positive numeric values - "Blues": [ - [0, "rgb(5,10,172)"], - [0.35, "rgb(40,60,190)"], - [0.5, "rgb(70,100,245)"], - [0.6, "rgb(90,120,245)"], - [0.7, "rgb(106,137,247)"], - [1, "rgb(220,220,220)"], - ], - "Picnic": [ - [0, "rgb(0,0,255)"], - [0.1, "rgb(51,153,255)"], - [0.2, "rgb(102,204,255)"], - [0.3, "rgb(153,204,255)"], - [0.4, "rgb(204,204,255)"], - [0.5, "rgb(255,255,255)"], - [0.6, "rgb(255,204,255)"], - [0.7, "rgb(255,153,255)"], - [0.8, "rgb(255,102,204)"], - [0.9, "rgb(255,102,102)"], - [1, "rgb(255,0,0)"], - ], - "Rainbow": [ - [0, "rgb(150,0,90)"], - [0.125, "rgb(0,0,200)"], - [0.25, "rgb(0,25,255)"], - [0.375, "rgb(0,152,255)"], - [0.5, "rgb(44,255,150)"], - [0.625, "rgb(151,255,0)"], - [0.75, "rgb(255,234,0)"], - [0.875, "rgb(255,111,0)"], - [1, "rgb(255,0,0)"], - ], - "Portland": [ - [0, "rgb(12,51,131)"], - [0.25, "rgb(10,136,186)"], - [0.5, "rgb(242,211,56)"], - [0.75, "rgb(242,143,56)"], - [1, "rgb(217,30,30)"], - ], - "Jet": [ - [0, "rgb(0,0,131)"], - [0.125, "rgb(0,60,170)"], - [0.375, "rgb(5,255,255)"], - [0.625, "rgb(255,255,0)"], - [0.875, "rgb(250,0,0)"], - [1, "rgb(128,0,0)"], - ], - "Hot": [ - [0, "rgb(0,0,0)"], - [0.3, "rgb(230,0,0)"], - [0.6, "rgb(255,210,0)"], - [1, "rgb(255,255,255)"], - ], - "Blackbody": [ - [0, "rgb(0,0,0)"], - [0.2, "rgb(230,0,0)"], - [0.4, "rgb(230,210,0)"], - [0.7, "rgb(255,255,255)"], - [1, "rgb(160,200,255)"], - ], - "Earth": [ - [0, "rgb(0,0,130)"], - [0.1, "rgb(0,180,180)"], - [0.2, "rgb(40,210,40)"], - [0.4, "rgb(230,230,50)"], - [0.6, "rgb(120,70,20)"], - [1, "rgb(255,255,255)"], - ], - "Electric": [ - [0, "rgb(0,0,0)"], - [0.15, "rgb(30,0,100)"], - [0.4, "rgb(120,0,100)"], - [0.6, "rgb(160,90,0)"], - [0.8, "rgb(230,200,0)"], - [1, "rgb(255,250,220)"], - ], - "Viridis": [ - [0, "#440154"], - [0.06274509803921569, "#48186a"], - [0.12549019607843137, "#472d7b"], - [0.18823529411764706, "#424086"], - [0.25098039215686274, "#3b528b"], - [0.3137254901960784, "#33638d"], - [0.3764705882352941, "#2c728e"], - [0.4392156862745098, "#26828e"], - [0.5019607843137255, "#21918c"], - [0.5647058823529412, "#1fa088"], - [0.6274509803921569, "#28ae80"], - [0.6901960784313725, "#3fbc73"], - [0.7529411764705882, "#5ec962"], - [0.8156862745098039, "#84d44b"], - [0.8784313725490196, "#addc30"], - [0.9411764705882353, "#d8e219"], - [1, "#fde725"], - ], - "Cividis": [ - [0.000000, "rgb(0,32,76)"], - [0.058824, "rgb(0,42,102)"], - [0.117647, "rgb(0,52,110)"], - [0.176471, "rgb(39,63,108)"], - [0.235294, "rgb(60,74,107)"], - [0.294118, "rgb(76,85,107)"], - [0.352941, "rgb(91,95,109)"], - [0.411765, "rgb(104,106,112)"], - [0.470588, "rgb(117,117,117)"], - [0.529412, "rgb(131,129,120)"], - [0.588235, "rgb(146,140,120)"], - [0.647059, "rgb(161,152,118)"], - [0.705882, "rgb(176,165,114)"], - [0.764706, "rgb(192,177,109)"], - [0.823529, "rgb(209,191,102)"], - [0.882353, "rgb(225,204,92)"], - [0.941176, "rgb(243,219,79)"], - [1.000000, "rgb(255,233,69)"], - ], -} - -# Create variable named after each scale that contains the sequence of colors only -for scale_name, scale_pairs in _plotlyjs_scales.items(): - scale_sequence = [c[1] for c in scale_pairs] - exec( - "{scale_name} = {scale_sequence}".format( - scale_name=scale_name, scale_sequence=scale_sequence - ) - ) +) +Bluered = [[0, "rgb(0,0,255)"], [1, "rgb(255,0,0)"]] +# modified RdBu based on +# http:#www.kennethmoreland.com/color-maps/ +RdBu = [ + [0, "rgb(5,10,172)"], + [0.35, "rgb(106,137,247)"], + [0.5, "rgb(190,190,190)"], + [0.6, "rgb(220,170,132)"], + [0.7, "rgb(230,145,90)"], + [1, "rgb(178,10,28)"], +] +# Scale for non-negative numeric values +Reds = [ + [0, "rgb(220,220,220)"], + [0.2, "rgb(245,195,157)"], + [0.4, "rgb(245,160,105)"], + [1, "rgb(178,10,28)"], +] +# Scale for non-positive numeric values +Blues = [ + [0, "rgb(5,10,172)"], + [0.35, "rgb(40,60,190)"], + [0.5, "rgb(70,100,245)"], + [0.6, "rgb(90,120,245)"], + [0.7, "rgb(106,137,247)"], + [1, "rgb(220,220,220)"], +] +Picnic = [ + [0, "rgb(0,0,255)"], + [0.1, "rgb(51,153,255)"], + [0.2, "rgb(102,204,255)"], + [0.3, "rgb(153,204,255)"], + [0.4, "rgb(204,204,255)"], + [0.5, "rgb(255,255,255)"], + [0.6, "rgb(255,204,255)"], + [0.7, "rgb(255,153,255)"], + [0.8, "rgb(255,102,204)"], + [0.9, "rgb(255,102,102)"], + [1, "rgb(255,0,0)"], +] +Rainbow = [ + [0, "rgb(150,0,90)"], + [0.125, "rgb(0,0,200)"], + [0.25, "rgb(0,25,255)"], + [0.375, "rgb(0,152,255)"], + [0.5, "rgb(44,255,150)"], + [0.625, "rgb(151,255,0)"], + [0.75, "rgb(255,234,0)"], + [0.875, "rgb(255,111,0)"], + [1, "rgb(255,0,0)"], +] +Portland = [ + [0, "rgb(12,51,131)"], + [0.25, "rgb(10,136,186)"], + [0.5, "rgb(242,211,56)"], + [0.75, "rgb(242,143,56)"], + [1, "rgb(217,30,30)"], +] +Jet = [ + [0, "rgb(0,0,131)"], + [0.125, "rgb(0,60,170)"], + [0.375, "rgb(5,255,255)"], + [0.625, "rgb(255,255,0)"], + [0.875, "rgb(250,0,0)"], + [1, "rgb(128,0,0)"], +] +Hot = [ + [0, "rgb(0,0,0)"], + [0.3, "rgb(230,0,0)"], + [0.6, "rgb(255,210,0)"], + [1, "rgb(255,255,255)"], +] +Blackbody = [ + [0, "rgb(0,0,0)"], + [0.2, "rgb(230,0,0)"], + [0.4, "rgb(230,210,0)"], + [0.7, "rgb(255,255,255)"], + [1, "rgb(160,200,255)"], +] +Earth = [ + [0, "rgb(0,0,130)"], + [0.1, "rgb(0,180,180)"], + [0.2, "rgb(40,210,40)"], + [0.4, "rgb(230,230,50)"], + [0.6, "rgb(120,70,20)"], + [1, "rgb(255,255,255)"], +] +Electric = [ + [0, "rgb(0,0,0)"], + [0.15, "rgb(30,0,100)"], + [0.4, "rgb(120,0,100)"], + [0.6, "rgb(160,90,0)"], + [0.8, "rgb(230,200,0)"], + [1, "rgb(255,250,220)"], +] +Viridis = [ + [0, "#440154"], + [0.06274509803921569, "#48186a"], + [0.12549019607843137, "#472d7b"], + [0.18823529411764706, "#424086"], + [0.25098039215686274, "#3b528b"], + [0.3137254901960784, "#33638d"], + [0.3764705882352941, "#2c728e"], + [0.4392156862745098, "#26828e"], + [0.5019607843137255, "#21918c"], + [0.5647058823529412, "#1fa088"], + [0.6274509803921569, "#28ae80"], + [0.6901960784313725, "#3fbc73"], + [0.7529411764705882, "#5ec962"], + [0.8156862745098039, "#84d44b"], + [0.8784313725490196, "#addc30"], + [0.9411764705882353, "#d8e219"], + [1, "#fde725"], +] +Cividis = [ + [0.000000, "rgb(0,32,76)"], + [0.058824, "rgb(0,42,102)"], + [0.117647, "rgb(0,52,110)"], + [0.176471, "rgb(39,63,108)"], + [0.235294, "rgb(60,74,107)"], + [0.294118, "rgb(76,85,107)"], + [0.352941, "rgb(91,95,109)"], + [0.411765, "rgb(104,106,112)"], + [0.470588, "rgb(117,117,117)"], + [0.529412, "rgb(131,129,120)"], + [0.588235, "rgb(146,140,120)"], + [0.647059, "rgb(161,152,118)"], + [0.705882, "rgb(176,165,114)"], + [0.764706, "rgb(192,177,109)"], + [0.823529, "rgb(209,191,102)"], + [0.882353, "rgb(225,204,92)"], + [0.941176, "rgb(243,219,79)"], + [1.000000, "rgb(255,233,69)"], +] -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] +Blackbody_r = Blackbody[::-1] +Bluered_r = Bluered[::-1] +Blues_r = Blues[::-1] +Cividis_r = Cividis[::-1] +Earth_r = Earth[::-1] +Electric_r = Electric[::-1] +Greens_r = Greens[::-1] +Greys_r = Greys[::-1] +Hot_r = Hot[::-1] +Jet_r = Jet[::-1] +Picnic_r = Picnic[::-1] +Portland_r = Portland[::-1] +Rainbow_r = Rainbow[::-1] +RdBu_r = RdBu[::-1] +Reds_r = Reds[::-1] +Viridis_r = Viridis[::-1] +YlGnBu_r = YlGnBu[::-1] +YlOrRd_r = YlOrRd[::-1] diff --git a/packages/python/plotly/_plotly_utils/colors/qualitative.py b/packages/python/plotly/_plotly_utils/colors/qualitative.py index ff8430f4695..a8ed54b367d 100644 --- a/packages/python/plotly/_plotly_utils/colors/qualitative.py +++ b/packages/python/plotly/_plotly_utils/colors/qualitative.py @@ -143,15 +143,42 @@ def swatches(template=None): "#E48F72", ] -from .colorbrewer import Set1, Pastel1, Dark2, Set2, Pastel2, Set3 # noqa: F401 -from .carto import Antique, Bold, Pastel, Prism, Safe, Vivid # noqa: F401 +Alphabet_r = Alphabet[::-1] +D3_r = D3[::-1] +Dark24_r = Dark24[::-1] +G10_r = G10[::-1] +Light24_r = Light24[::-1] +Plotly_r = Plotly[::-1] +T10_r = T10[::-1] -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] +from .colorbrewer import ( # noqa: F401 + Set1, + Pastel1, + Dark2, + Set2, + Pastel2, + Set3, + Set1_r, + Pastel1_r, + Dark2_r, + Set2_r, + Pastel2_r, + Set3_r, +) +from .carto import ( # noqa: F401 + Antique, + Bold, + Pastel, + Prism, + Safe, + Vivid, + Antique_r, + Bold_r, + Pastel_r, + Prism_r, + Safe_r, + Vivid_r, +) __all__ = ["swatches"] diff --git a/packages/python/plotly/_plotly_utils/colors/sequential.py b/packages/python/plotly/_plotly_utils/colors/sequential.py index 50d77eaaac1..e14d33c13da 100644 --- a/packages/python/plotly/_plotly_utils/colors/sequential.py +++ b/packages/python/plotly/_plotly_utils/colors/sequential.py @@ -115,7 +115,29 @@ def swatches_continuous(template=None): "#b11901", "#7a0402", ] -from .plotlyjs import Blackbody, Bluered, Electric, Hot, Jet, Rainbow # noqa: F401 + +Cividis_r = Cividis[::-1] +Inferno_r = Inferno[::-1] +Magma_r = Magma[::-1] +Plasma_r = Plasma[::-1] +Plotly3_r = Plotly3[::-1] +Turbo_r = Turbo[::-1] +Viridis_r = Viridis[::-1] + +from .plotlyjs import ( # noqa: F401 + Blackbody, + Bluered, + Electric, + Hot, + Jet, + Rainbow, + Blackbody_r, + Bluered_r, + Electric_r, + Hot_r, + Jet_r, + Rainbow_r, +) from .colorbrewer import ( # noqa: F401 Blues, @@ -137,6 +159,25 @@ def swatches_continuous(template=None): YlGnBu, YlOrBr, YlOrRd, + Blues_r, + BuGn_r, + BuPu_r, + GnBu_r, + Greens_r, + Greys_r, + OrRd_r, + Oranges_r, + PuBu_r, + PuBuGn_r, + PuRd_r, + Purples_r, + RdBu_r, + RdPu_r, + Reds_r, + YlGn_r, + YlGnBu_r, + YlOrBr_r, + YlOrRd_r, ) from .cmocean import ( # noqa: F401 @@ -153,6 +194,19 @@ def swatches_continuous(template=None): speed, amp, tempo, + turbid_r, + thermal_r, + haline_r, + solar_r, + ice_r, + gray_r, + deep_r, + dense_r, + algae_r, + matter_r, + speed_r, + amp_r, + tempo_r, ) from .carto import ( # noqa: F401 @@ -177,14 +231,27 @@ def swatches_continuous(template=None): Sunsetdark, Agsunset, Brwnyl, + Burg_r, + Burgyl_r, + Redor_r, + Oryel_r, + Peach_r, + Pinkyl_r, + Mint_r, + Blugrn_r, + Darkmint_r, + Emrld_r, + Aggrnyl_r, + Bluyl_r, + Teal_r, + Tealgrn_r, + Purp_r, + Purpor_r, + Sunset_r, + Magenta_r, + Sunsetdark_r, + Agsunset_r, + Brwnyl_r, ) -# 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.startswith("swatches") or _k.endswith("_r"): - continue - globals()[_k + "_r"] = _cols[::-1] - - __all__ = ["swatches"] diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py index 36bde27d1f1..85e1dc00bf0 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py @@ -1,4 +1,5 @@ import plotly.express as px +import inspect def test_reversed_colorscale(): @@ -24,3 +25,31 @@ def test_reversed_colorscale(): colors1 = [val[1] for val in fig1.layout.coloraxis.colorscale] colors2 = [val[1] for val in fig2.layout.coloraxis.colorscale] assert colors1 == colors2[::-1] + + +def test_r_colorscales(): + + for colorscale_members in [ + inspect.getmembers(px.colors.sequential), + inspect.getmembers(px.colors.diverging), + inspect.getmembers(px.colors.cyclical), + inspect.getmembers(px.colors.qualitative), + inspect.getmembers(px.colors.carto), + inspect.getmembers(px.colors.cmocean), + inspect.getmembers(px.colors.colorbrewer), + inspect.getmembers(px.colors.plotlyjs), + ]: + scale_names = [ + c[0] + for c in colorscale_members + if isinstance(c, tuple) + and len(c) == 2 + and isinstance(c[0], str) + and isinstance(c[1], list) + and not c[0].startswith("_") + ] + for scale in scale_names: + if scale.endswith("_r"): + assert scale.replace("_r", "") in scale_names + else: + assert scale + "_r" in scale_names From a2f94ab57b52d3ff7342d9b26050177cde5ea4ee Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Fri, 6 May 2022 15:10:28 -0400 Subject: [PATCH 2/2] fix inlining of plotlyjs.py --- .../plotly/_plotly_utils/colors/plotlyjs.py | 298 +++++++++--------- 1 file changed, 143 insertions(+), 155 deletions(-) diff --git a/packages/python/plotly/_plotly_utils/colors/plotlyjs.py b/packages/python/plotly/_plotly_utils/colors/plotlyjs.py index 3a0517e3c3c..a49ae023e5c 100644 --- a/packages/python/plotly/_plotly_utils/colors/plotlyjs.py +++ b/packages/python/plotly/_plotly_utils/colors/plotlyjs.py @@ -1,175 +1,163 @@ # Copied from # https://github.com/plotly/plotly.js/blob/master/src/components/colorscale/scales.js -Greys = [[0, "rgb(0,0,0)"], [1, "rgb(255,255,255)"]] -YlGnBu = [ - [0, "rgb(8,29,88)"], - [0.125, "rgb(37,52,148)"], - [0.25, "rgb(34,94,168)"], - [0.375, "rgb(29,145,192)"], - [0.5, "rgb(65,182,196)"], - [0.625, "rgb(127,205,187)"], - [0.75, "rgb(199,233,180)"], - [0.875, "rgb(237,248,217)"], - [1, "rgb(255,255,217)"], -] -Greens = [ - [0, "rgb(0,68,27)"], - [0.125, "rgb(0,109,44)"], - [0.25, "rgb(35,139,69)"], - [0.375, "rgb(65,171,93)"], - [0.5, "rgb(116,196,118)"], - [0.625, "rgb(161,217,155)"], - [0.75, "rgb(199,233,192)"], - [0.875, "rgb(229,245,224)"], - [1, "rgb(247,252,245)"], -] -YlOrRd = ( - [ - [0, "rgb(128,0,38)"], - [0.125, "rgb(189,0,38)"], - [0.25, "rgb(227,26,28)"], - [0.375, "rgb(252,78,42)"], - [0.5, "rgb(253,141,60)"], - [0.625, "rgb(254,178,76)"], - [0.75, "rgb(254,217,118)"], - [0.875, "rgb(255,237,160)"], - [1, "rgb(255,255,204)"], - ], -) -Bluered = [[0, "rgb(0,0,255)"], [1, "rgb(255,0,0)"]] -# modified RdBu based on -# http:#www.kennethmoreland.com/color-maps/ -RdBu = [ - [0, "rgb(5,10,172)"], - [0.35, "rgb(106,137,247)"], - [0.5, "rgb(190,190,190)"], - [0.6, "rgb(220,170,132)"], - [0.7, "rgb(230,145,90)"], - [1, "rgb(178,10,28)"], -] -# Scale for non-negative numeric values -Reds = [ - [0, "rgb(220,220,220)"], - [0.2, "rgb(245,195,157)"], - [0.4, "rgb(245,160,105)"], - [1, "rgb(178,10,28)"], +# NOTE: these differ slightly from plotly.colors.PLOTLY_SCALES from Plotly.js because +# those ones don't have perfectly evenly spaced steps ... +# not sure when this skew was introduced, possibly as early as Plotly.py v4.0 + +Blackbody = [ + "rgb(0,0,0)", + "rgb(230,0,0)", + "rgb(230,210,0)", + "rgb(255,255,255)", + "rgb(160,200,255)", ] -# Scale for non-positive numeric values +Bluered = ["rgb(0,0,255)", "rgb(255,0,0)"] Blues = [ - [0, "rgb(5,10,172)"], - [0.35, "rgb(40,60,190)"], - [0.5, "rgb(70,100,245)"], - [0.6, "rgb(90,120,245)"], - [0.7, "rgb(106,137,247)"], - [1, "rgb(220,220,220)"], + "rgb(5,10,172)", + "rgb(40,60,190)", + "rgb(70,100,245)", + "rgb(90,120,245)", + "rgb(106,137,247)", + "rgb(220,220,220)", ] -Picnic = [ - [0, "rgb(0,0,255)"], - [0.1, "rgb(51,153,255)"], - [0.2, "rgb(102,204,255)"], - [0.3, "rgb(153,204,255)"], - [0.4, "rgb(204,204,255)"], - [0.5, "rgb(255,255,255)"], - [0.6, "rgb(255,204,255)"], - [0.7, "rgb(255,153,255)"], - [0.8, "rgb(255,102,204)"], - [0.9, "rgb(255,102,102)"], - [1, "rgb(255,0,0)"], +Cividis = [ + "rgb(0,32,76)", + "rgb(0,42,102)", + "rgb(0,52,110)", + "rgb(39,63,108)", + "rgb(60,74,107)", + "rgb(76,85,107)", + "rgb(91,95,109)", + "rgb(104,106,112)", + "rgb(117,117,117)", + "rgb(131,129,120)", + "rgb(146,140,120)", + "rgb(161,152,118)", + "rgb(176,165,114)", + "rgb(192,177,109)", + "rgb(209,191,102)", + "rgb(225,204,92)", + "rgb(243,219,79)", + "rgb(255,233,69)", ] -Rainbow = [ - [0, "rgb(150,0,90)"], - [0.125, "rgb(0,0,200)"], - [0.25, "rgb(0,25,255)"], - [0.375, "rgb(0,152,255)"], - [0.5, "rgb(44,255,150)"], - [0.625, "rgb(151,255,0)"], - [0.75, "rgb(255,234,0)"], - [0.875, "rgb(255,111,0)"], - [1, "rgb(255,0,0)"], +Earth = [ + "rgb(0,0,130)", + "rgb(0,180,180)", + "rgb(40,210,40)", + "rgb(230,230,50)", + "rgb(120,70,20)", + "rgb(255,255,255)", ] -Portland = [ - [0, "rgb(12,51,131)"], - [0.25, "rgb(10,136,186)"], - [0.5, "rgb(242,211,56)"], - [0.75, "rgb(242,143,56)"], - [1, "rgb(217,30,30)"], +Electric = [ + "rgb(0,0,0)", + "rgb(30,0,100)", + "rgb(120,0,100)", + "rgb(160,90,0)", + "rgb(230,200,0)", + "rgb(255,250,220)", +] +Greens = [ + "rgb(0,68,27)", + "rgb(0,109,44)", + "rgb(35,139,69)", + "rgb(65,171,93)", + "rgb(116,196,118)", + "rgb(161,217,155)", + "rgb(199,233,192)", + "rgb(229,245,224)", + "rgb(247,252,245)", ] +Greys = ["rgb(0,0,0)", "rgb(255,255,255)"] +Hot = ["rgb(0,0,0)", "rgb(230,0,0)", "rgb(255,210,0)", "rgb(255,255,255)"] Jet = [ - [0, "rgb(0,0,131)"], - [0.125, "rgb(0,60,170)"], - [0.375, "rgb(5,255,255)"], - [0.625, "rgb(255,255,0)"], - [0.875, "rgb(250,0,0)"], - [1, "rgb(128,0,0)"], + "rgb(0,0,131)", + "rgb(0,60,170)", + "rgb(5,255,255)", + "rgb(255,255,0)", + "rgb(250,0,0)", + "rgb(128,0,0)", ] -Hot = [ - [0, "rgb(0,0,0)"], - [0.3, "rgb(230,0,0)"], - [0.6, "rgb(255,210,0)"], - [1, "rgb(255,255,255)"], +Picnic = [ + "rgb(0,0,255)", + "rgb(51,153,255)", + "rgb(102,204,255)", + "rgb(153,204,255)", + "rgb(204,204,255)", + "rgb(255,255,255)", + "rgb(255,204,255)", + "rgb(255,153,255)", + "rgb(255,102,204)", + "rgb(255,102,102)", + "rgb(255,0,0)", ] -Blackbody = [ - [0, "rgb(0,0,0)"], - [0.2, "rgb(230,0,0)"], - [0.4, "rgb(230,210,0)"], - [0.7, "rgb(255,255,255)"], - [1, "rgb(160,200,255)"], +Portland = [ + "rgb(12,51,131)", + "rgb(10,136,186)", + "rgb(242,211,56)", + "rgb(242,143,56)", + "rgb(217,30,30)", ] -Earth = [ - [0, "rgb(0,0,130)"], - [0.1, "rgb(0,180,180)"], - [0.2, "rgb(40,210,40)"], - [0.4, "rgb(230,230,50)"], - [0.6, "rgb(120,70,20)"], - [1, "rgb(255,255,255)"], +Rainbow = [ + "rgb(150,0,90)", + "rgb(0,0,200)", + "rgb(0,25,255)", + "rgb(0,152,255)", + "rgb(44,255,150)", + "rgb(151,255,0)", + "rgb(255,234,0)", + "rgb(255,111,0)", + "rgb(255,0,0)", ] -Electric = [ - [0, "rgb(0,0,0)"], - [0.15, "rgb(30,0,100)"], - [0.4, "rgb(120,0,100)"], - [0.6, "rgb(160,90,0)"], - [0.8, "rgb(230,200,0)"], - [1, "rgb(255,250,220)"], +RdBu = [ + "rgb(5,10,172)", + "rgb(106,137,247)", + "rgb(190,190,190)", + "rgb(220,170,132)", + "rgb(230,145,90)", + "rgb(178,10,28)", ] +Reds = ["rgb(220,220,220)", "rgb(245,195,157)", "rgb(245,160,105)", "rgb(178,10,28)"] Viridis = [ - [0, "#440154"], - [0.06274509803921569, "#48186a"], - [0.12549019607843137, "#472d7b"], - [0.18823529411764706, "#424086"], - [0.25098039215686274, "#3b528b"], - [0.3137254901960784, "#33638d"], - [0.3764705882352941, "#2c728e"], - [0.4392156862745098, "#26828e"], - [0.5019607843137255, "#21918c"], - [0.5647058823529412, "#1fa088"], - [0.6274509803921569, "#28ae80"], - [0.6901960784313725, "#3fbc73"], - [0.7529411764705882, "#5ec962"], - [0.8156862745098039, "#84d44b"], - [0.8784313725490196, "#addc30"], - [0.9411764705882353, "#d8e219"], - [1, "#fde725"], + "#440154", + "#48186a", + "#472d7b", + "#424086", + "#3b528b", + "#33638d", + "#2c728e", + "#26828e", + "#21918c", + "#1fa088", + "#28ae80", + "#3fbc73", + "#5ec962", + "#84d44b", + "#addc30", + "#d8e219", + "#fde725", ] -Cividis = [ - [0.000000, "rgb(0,32,76)"], - [0.058824, "rgb(0,42,102)"], - [0.117647, "rgb(0,52,110)"], - [0.176471, "rgb(39,63,108)"], - [0.235294, "rgb(60,74,107)"], - [0.294118, "rgb(76,85,107)"], - [0.352941, "rgb(91,95,109)"], - [0.411765, "rgb(104,106,112)"], - [0.470588, "rgb(117,117,117)"], - [0.529412, "rgb(131,129,120)"], - [0.588235, "rgb(146,140,120)"], - [0.647059, "rgb(161,152,118)"], - [0.705882, "rgb(176,165,114)"], - [0.764706, "rgb(192,177,109)"], - [0.823529, "rgb(209,191,102)"], - [0.882353, "rgb(225,204,92)"], - [0.941176, "rgb(243,219,79)"], - [1.000000, "rgb(255,233,69)"], +YlGnBu = [ + "rgb(8,29,88)", + "rgb(37,52,148)", + "rgb(34,94,168)", + "rgb(29,145,192)", + "rgb(65,182,196)", + "rgb(127,205,187)", + "rgb(199,233,180)", + "rgb(237,248,217)", + "rgb(255,255,217)", +] +YlOrRd = [ + "rgb(128,0,38)", + "rgb(189,0,38)", + "rgb(227,26,28)", + "rgb(252,78,42)", + "rgb(253,141,60)", + "rgb(254,178,76)", + "rgb(254,217,118)", + "rgb(255,237,160)", + "rgb(255,255,204)", ] Blackbody_r = Blackbody[::-1]