Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3af2220

Browse files
committed
Allowed for lists of more than 2 colors in trisurf, updated tests, altered some color type-swapping functions
1 parent 13a84a4 commit 3af2220

File tree

2 files changed

+137
-76
lines changed

2 files changed

+137
-76
lines changed

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -627,18 +627,6 @@ def test_valid_colormap(self):
627627
self.assertRaises(PlotlyError, tls.FigureFactory.create_trisurf,
628628
x, y, z, simplices, colormap='foo')
629629

630-
# check that colormap is a list, if not a string
631-
632-
pattern1 = (
633-
"If 'colormap' is a list, then its items must be tripets of the "
634-
"form a,b,c or 'rgbx,y,z' where a,b,c are between 0 and 1 "
635-
"inclusive and x,y,z are between 0 and 255 inclusive."
636-
)
637-
638-
self.assertRaisesRegexp(PlotlyError, pattern1,
639-
tls.FigureFactory.create_trisurf,
640-
x, y, z, simplices, colormap=3)
641-
642630
# check: if colormap is a list of rgb color strings, make sure the
643631
# entries of each color are no greater than 255.0
644632

@@ -650,20 +638,31 @@ def test_valid_colormap(self):
650638
self.assertRaisesRegexp(PlotlyError, pattern2,
651639
tls.FigureFactory.create_trisurf,
652640
x, y, z, simplices,
653-
colormap=['rgb(1, 2, 3)', 'rgb(4, 5, 600)'])
641+
colormap=['rgb(4, 5, 600)'])
654642

655643
# check: if colormap is a list of tuple colors, make sure the entries
656644
# of each tuple are no greater than 1.0
657645

658646
pattern3 = (
659-
"Whoops! The elements in your rgb colormap tuples "
660-
"cannot exceed 1.0."
647+
"Whoops! The elements in your colormap tuples cannot exceed 1.0."
661648
)
662649

663650
self.assertRaisesRegexp(PlotlyError, pattern3,
664651
tls.FigureFactory.create_trisurf,
665652
x, y, z, simplices,
666-
colormap=[(0.2, 0.4, 0.6), (0.8, 1.0, 1.2)])
653+
colormap=[(0.8, 1.0, 1.2)])
654+
655+
# check:
656+
657+
pattern4 = (
658+
"You must input a valid colormap. Valid types include a plotly "
659+
"scale, rgb, hex or tuple color, or lastly a list of any color "
660+
"types."
661+
)
662+
663+
self.assertRaisesRegexp(PlotlyError, pattern4,
664+
tls.FigureFactory.create_trisurf,
665+
x, y, z, simplices, colormap=1)
667666

668667
def test_trisurf_all_args(self):
669668

plotly/tools.py

Lines changed: 122 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,15 +1480,24 @@ def _unconvert_from_RGB_255(colors):
14801480
between 0 and 1
14811481
14821482
"""
1483-
un_rgb_colors = []
1484-
for color in colors:
1485-
un_rgb_color = (color[0]/(255.0),
1486-
color[1]/(255.0),
1487-
color[2]/(255.0))
1483+
if isinstance(colors, tuple):
1484+
1485+
un_rgb_color = (colors[0]/(255.0),
1486+
colors[1]/(255.0),
1487+
colors[2]/(255.0))
1488+
1489+
return un_rgb_color
1490+
1491+
if isinstance(colors, list):
1492+
un_rgb_colors = []
1493+
for color in colors:
1494+
un_rgb_color = (color[0]/(255.0),
1495+
color[1]/(255.0),
1496+
color[2]/(255.0))
14881497

1489-
un_rgb_colors.append(un_rgb_color)
1498+
un_rgb_colors.append(un_rgb_color)
14901499

1491-
return un_rgb_colors
1500+
return un_rgb_colors
14921501

14931502
@staticmethod
14941503
def _map_z2color(zval, colormap, vmin, vmax):
@@ -1640,10 +1649,11 @@ def create_trisurf(x, y, z, simplices, colormap=None,
16401649
:param (array) simplices: an array of shape (ntri, 3) where ntri is
16411650
the number of triangles in the triangularization. Each row of the
16421651
array contains the indicies of the verticies of each triangle.
1643-
:param (str|list) colormap: either a plotly scale name, or a list
1644-
containing 2 triplets. These triplets must be of the form (a,b,c)
1645-
or 'rgb(x,y,z)' where a,b,c belong to the interval [0,1] and x,y,z
1646-
belong to [0,255]
1652+
:param (str|tuple|list) colormap: takes a plotly scale, an rgb or hex
1653+
string, a tuple, or a list of colors of the aforementioned rgb,
1654+
hex or tuple types. An rgb/triplet color type is a triplet of the
1655+
form (a,b,c) or 'rgb(x,y,z)' respectively where a,b,c belong to
1656+
the interval [0,1] and x,y,z belong to [0,255]
16471657
:param (function) dist_func: The function that determines how the
16481658
coloring of the surface changes. It takes 3 arguments x, y, z and
16491659
must return a formula of these variables which can include numpy
@@ -1794,7 +1804,9 @@ def dist_origin(x, y, z):
17941804
17951805
# Create a figure
17961806
fig1 = FF.create_trisurf(x=x, y=y, z=z,
1797-
colormap="Blues",
1807+
colormap=['#604d9e',
1808+
'rgb(50, 150, 255)',
1809+
(0.2, 0.2, 0.8)],
17981810
simplices=simplices,
17991811
dist_func=dist_origin)
18001812
# Plot the data
@@ -1832,52 +1844,76 @@ def dist_origin(x, y, z):
18321844
colormap = plotly_scales[colormap]
18331845
colormap = FigureFactory._unlabel_rgb(colormap)
18341846
colormap = FigureFactory._unconvert_from_RGB_255(colormap)
1835-
else:
1836-
if ('rgb' not in colormap) and ('#' not in colormap):
1837-
scale_keys = list(plotly_scales.keys())
1838-
raise exceptions.PlotlyError("If you input a string "
1839-
"for 'colormap', it must "
1840-
"either be a Plotly "
1841-
"colorscale, an 'rgb' "
1842-
"color or a hex color."
1843-
"Valud plotly colorscale "
1844-
"names are {}".format(scale_keys))
1845-
# put colormap string into a list
1847+
1848+
elif 'rgb' in colormap:
1849+
# put colormap in list
18461850
colors_list = []
18471851
colors_list.append(colormap)
18481852
colormap = colors_list
1853+
18491854
colormap = FigureFactory._unlabel_rgb(colormap)
18501855
colormap = FigureFactory._unconvert_from_RGB_255(colormap)
18511856

1852-
else:
1853-
if not isinstance(colormap, list):
1854-
raise exceptions.PlotlyError("If 'colormap' is a list, then "
1855-
"its items must be tripets of "
1856-
"the form a,b,c or 'rgbx,y,z' "
1857-
"where a,b,c are between 0 and "
1858-
"1 inclusive and x,y,z are "
1859-
"between 0 and 255 inclusive.")
1860-
if 'rgb' in colormap[0]:
1861-
colormap = FigureFactory._unlabel_rgb(colormap)
1862-
for color in colormap:
1863-
for index in range(3):
1864-
if color[index] > 255.0:
1857+
elif '#' in colormap:
1858+
colormap = FigureFactory._hex_to_rgb(colormap)
1859+
colormap = FigureFactory._unconvert_from_RGB_255(colormap)
1860+
1861+
# put colormap in list
1862+
colors_list = []
1863+
colors_list.append(colormap)
1864+
colormap = colors_list
1865+
1866+
else:
1867+
scale_keys = list(plotly_scales.keys())
1868+
raise exceptions.PlotlyError("If you input a string "
1869+
"for 'colormap', it must "
1870+
"either be a Plotly "
1871+
"colorscale, an 'rgb' "
1872+
"color or a hex color."
1873+
"Valud plotly colorscale "
1874+
"names are {}".format(scale_keys))
1875+
elif isinstance(colormap, tuple):
1876+
colors_list = []
1877+
colors_list.append(colormap)
1878+
colormap = colors_list
1879+
1880+
elif isinstance(colormap, list):
1881+
new_colormap = []
1882+
for color in colormap:
1883+
if 'rgb' in color:
1884+
color = FigureFactory._unlabel_rgb(color)
1885+
1886+
for value in color:
1887+
if value > 255.0:
18651888
raise exceptions.PlotlyError("Whoops! The "
18661889
"elements in your "
18671890
"rgb colormap "
18681891
"tuples cannot "
18691892
"exceed 255.0.")
1870-
colormap = FigureFactory._unconvert_from_RGB_255(colormap)
18711893

1872-
if isinstance(colormap[0], tuple):
1873-
for color in colormap:
1874-
for index in range(3):
1875-
if color[index] > 1.0:
1894+
color = FigureFactory._unconvert_from_RGB_255(color)
1895+
new_colormap.append(color)
1896+
elif '#' in color:
1897+
color = FigureFactory._hex_to_rgb(color)
1898+
color = FigureFactory._unconvert_from_RGB_255(color)
1899+
new_colormap.append(color)
1900+
elif isinstance(color, tuple):
1901+
1902+
for value in color:
1903+
if value > 1.0:
18761904
raise exceptions.PlotlyError("Whoops! The "
1877-
"elements in your "
1878-
"rgb colormap "
1905+
"elements in "
1906+
"your colormap "
18791907
"tuples cannot "
18801908
"exceed 1.0.")
1909+
new_colormap.append(color)
1910+
colormap = new_colormap
1911+
1912+
else:
1913+
raise exceptions.PlotlyError("You must input a valid colormap. "
1914+
"Valid types include a plotly scale, "
1915+
"rgb, hex or tuple color, or lastly "
1916+
"a list of any color types.")
18811917

18821918
data1 = FigureFactory._trisurf(x, y, z, simplices,
18831919
dist_func=dist_func,
@@ -2907,16 +2943,20 @@ def _label_rgb(colors):
29072943
"""
29082944
Takes colors (a, b, c) and returns tuples 'rgb(a, b, c)'
29092945
2910-
Takes a list of two color tuples of the form (a, b, c) and returns the
2911-
same list with each tuple replaced by a string 'rgb(a, b, c)'
2946+
Takes either a list or a single color tuple of the form (a, b, c) and
2947+
returns the same colors(s) with each tuple replaced by a string
2948+
'rgb(a, b, c)'
29122949
29132950
"""
2914-
colors_label = []
2915-
for color in colors:
2916-
color_label = 'rgb{}'.format(color)
2917-
colors_label.append(color_label)
2951+
if isinstance(colors, tuple):
2952+
return 'rgb{}'.format(colors)
2953+
else:
2954+
colors_label = []
2955+
for color in colors:
2956+
color_label = 'rgb{}'.format(color)
2957+
colors_label.append(color_label)
29182958

2919-
return colors_label
2959+
return colors_label
29202960

29212961
@staticmethod
29222962
def _unlabel_rgb(colors):
@@ -2929,16 +2969,15 @@ def _unlabel_rgb(colors):
29292969
(a, b, c)
29302970
29312971
"""
2932-
unlabelled_colors = []
2933-
for character in colors:
2972+
if isinstance(colors, str):
29342973
str_vals = ''
2935-
for index in range(len(character)):
2974+
for index in range(len(colors)):
29362975
try:
2937-
float(character[index])
2938-
str_vals = str_vals + character[index]
2976+
float(colors[index])
2977+
str_vals = str_vals + colors[index]
29392978
except ValueError:
2940-
if (character[index] == ',') or (character[index] == '.'):
2941-
str_vals = str_vals + character[index]
2979+
if (colors[index] == ',') or (colors[index] == '.'):
2980+
str_vals = str_vals + colors[index]
29422981

29432982
str_vals = str_vals + ','
29442983
numbers = []
@@ -2949,10 +2988,33 @@ def _unlabel_rgb(colors):
29492988
else:
29502989
numbers.append(float(str_num))
29512990
str_num = ''
2952-
unlabelled_tuple = (numbers[0], numbers[1], numbers[2])
2953-
unlabelled_colors.append(unlabelled_tuple)
2991+
return (numbers[0], numbers[1], numbers[2])
2992+
2993+
if isinstance(colors, list):
2994+
unlabelled_colors = []
2995+
for color in colors:
2996+
str_vals = ''
2997+
for index in range(len(color)):
2998+
try:
2999+
float(color[index])
3000+
str_vals = str_vals + color[index]
3001+
except ValueError:
3002+
if (color[index] == ',') or (color[index] == '.'):
3003+
str_vals = str_vals + color[index]
3004+
3005+
str_vals = str_vals + ','
3006+
numbers = []
3007+
str_num = ''
3008+
for char in str_vals:
3009+
if char != ',':
3010+
str_num = str_num + char
3011+
else:
3012+
numbers.append(float(str_num))
3013+
str_num = ''
3014+
unlabelled_tuple = (numbers[0], numbers[1], numbers[2])
3015+
unlabelled_colors.append(unlabelled_tuple)
29543016

2955-
return unlabelled_colors
3017+
return unlabelled_colors
29563018

29573019
@staticmethod
29583020
def create_scatterplotmatrix(df, dataframe=None, headers=None,

0 commit comments

Comments
 (0)