Skip to content

Commit f9f3bdc

Browse files
committed
updated tests in colors.py and replace proper trisurf_all_args in test_optional
1 parent 920d304 commit f9f3bdc

File tree

4 files changed

+78
-82
lines changed

4 files changed

+78
-82
lines changed

plotly/colors.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,10 @@ def validate_colors(colors, colors_list=None):
233233
return
234234

235235

236-
def convert_colors_to_same_type(colors, colortype='rgb',
237-
scale=None, colors_list=None):
236+
def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
237+
return_default_colors=False,
238+
num_of_defualt_colors=2,
239+
colors_list=None):
238240
"""
239241
Converts color(s) to the specified color type
240242
@@ -243,11 +245,15 @@ def convert_colors_to_same_type(colors, colortype='rgb',
243245
rgb or tuple color type, aswell as the scale as the second element. If
244246
colors is a Plotly Scale name, then 'scale' will be forced to the scale
245247
from the respective colorscale and the colors in that colorscale will also
246-
be coverted to the selected colortype.
248+
be coverted to the selected colortype. If colors is None, then there is an
249+
option to return portion of the DEFAULT_PLOTLY_COLORS
247250
"""
248251
if colors_list is None:
249252
colors_list = []
250253

254+
if colors is None and return_default_colors is True:
255+
colors_list = DEFAULT_PLOTLY_COLORS[0:num_of_defualt_colors]
256+
251257
if isinstance(colors, str):
252258
if colors in PLOTLY_SCALES:
253259
colors_list = colorscale_to_colors(PLOTLY_SCALES[colors])

plotly/tests/test_core/test_colors/test_colors.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ def test_validate_colors(self):
3939

4040
def test_convert_colors_to_same_type(self):
4141

42-
# test string input
43-
color_string = 'foo'
42+
# test colortype
43+
color_tuple = ['#aaaaaa', '#bbbbbb', '#cccccc']
44+
scale = [0, 1]
4445

45-
pattern = ("If your colors variable is a string, it must be a "
46-
"Plotly scale, an rgb color or a hex color.")
47-
48-
self.assertRaisesRegexp(PlotlyError, pattern,
49-
colors.convert_colors_to_same_type,
50-
color_string)
46+
self.assertRaises(PlotlyError, colors.convert_colors_to_same_type,
47+
color_tuple, scale=scale)
5148

5249
# test colortype
5350
color_tuple = (1, 1, 1)
@@ -73,6 +70,38 @@ def test_convert_dict_colors_to_same_type(self):
7370
colors.convert_dict_colors_to_same_type,
7471
color_dict, colortype)
7572

73+
def test_validate_scale_values(self):
74+
75+
# test that scale length is at least 2
76+
scale = [0]
77+
78+
pattern = ("You must input a list of scale values that has at least "
79+
"two values.")
80+
81+
self.assertRaisesRegexp(PlotlyError, pattern,
82+
colors.validate_scale_values,
83+
scale)
84+
85+
# test if first and last number is 0 and 1 respectively
86+
scale = [0, 1.1]
87+
88+
pattern = ("The first and last number in your scale must be 0.0 and "
89+
"1.0 respectively.")
90+
91+
self.assertRaisesRegexp(PlotlyError, pattern,
92+
colors.validate_scale_values,
93+
scale)
94+
95+
# test numbers increase
96+
scale = [0, 2, 1]
97+
98+
pattern = ("'scale' must be a list that contains an increasing "
99+
"sequence of numbers.")
100+
101+
self.assertRaisesRegexp(PlotlyError, pattern,
102+
colors.validate_scale_values,
103+
scale)
104+
76105
def test_make_colorscale(self):
77106

78107
# test minimum colors length
@@ -93,23 +122,3 @@ def test_make_colorscale(self):
93122

94123
self.assertRaisesRegexp(PlotlyError, pattern2, colors.make_colorscale,
95124
color_list2, scale)
96-
97-
# test first and last number of scale
98-
scale2 = [0, 2]
99-
100-
pattern3 = ("The first and last number in scale must be 0.0 and 1.0 "
101-
"respectively.")
102-
103-
self.assertRaisesRegexp(PlotlyError, pattern3, colors.make_colorscale,
104-
color_list2, scale2)
105-
106-
# test for strictly increasing scale
107-
color_list3 = [(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)]
108-
scale3 = [0, 1, 1]
109-
110-
pattern4 = ("'scale' must be a list that contains an increasing "
111-
"sequence of numbers where the first and last number are"
112-
"0.0 and 1.0 respectively.")
113-
114-
self.assertRaisesRegexp(PlotlyError, pattern4, colors.make_colorscale,
115-
color_list3, scale3)

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -842,70 +842,50 @@ def test_trisurf_all_args(self):
842842
)
843843

844844
exp_trisurf_plot = {
845-
'data': [{'facecolor': ['rgb(144, 94, 132)',
846-
'rgb(23, 190, 207)',
847-
'rgb(144, 94, 132)',
848-
'rgb(31, 119, 180)',
849-
'rgb(144, 94, 132)',
850-
'rgb(31, 119, 180)',
851-
'rgb(144, 94, 132)',
852-
'rgb(23, 190, 207)'],
853-
'i': np.array([3, 1, 1, 5, 7, 3, 5, 7]),
854-
'j': np.array([1, 3, 5, 1, 3, 7, 7, 5]),
855-
'k': np.array([4, 0, 4, 2, 4, 6, 4, 8]),
845+
'data': [{'facecolor': ['rgb(143, 123, 97)', 'rgb(255, 127, 14)',
846+
'rgb(143, 123, 97)', 'rgb(31, 119, 180)',
847+
'rgb(143, 123, 97)', 'rgb(31, 119, 180)',
848+
'rgb(143, 123, 97)', 'rgb(255, 127, 14)'],
849+
'i': [3, 1, 1, 5, 7, 3, 5, 7],
850+
'j': [1, 3, 5, 1, 3, 7, 7, 5],
851+
'k': [4, 0, 4, 2, 4, 6, 4, 8],
856852
'name': '',
857853
'type': 'mesh3d',
858-
'x': np.array([-1., 0., 1., -1., 0.,
859-
1., -1., 0., 1.]),
860-
'y': np.array([-1., -1., -1., 0., 0.,
861-
0., 1., 1., 1.]),
862-
'z': np.array([1., -0., -1., -0., 0.,
863-
0., -1., 0., 1.])},
854+
'x': [-1., 0., 1., -1., 0., 1., -1., 0., 1.],
855+
'y': [-1., -1., -1., 0., 0., 0., 1., 1., 1.],
856+
'z': [1., -0., -1., -0., 0., 0., -1., 0., 1.]},
864857
{'line': {'color': 'rgb(50, 50, 50)', 'width': 1.5},
865858
'mode': 'lines',
866859
'showlegend': False,
867860
'type': 'scatter3d',
868-
'x': np.array([-1.0, 0.0, 0.0, -1.0, None, 0.0, -1.0,
869-
-1.0, 0.0, None, 0.0, 1.0, 0.0, 0.0,
870-
None, 1.0, 0.0, 1.0, 1.0, None, 0.0,
871-
-1.0, 0.0, 0.0, None, -1.0, 0.0, -1.0,
872-
-1.0, None, 1.0, 0.0, 0.0, 1.0, None,
873-
0.0, 1.0, 1.0, 0.0, None]),
874-
'y': np.array([0.0, -1.0, 0.0, 0.0, None, -1.0, 0.0,
875-
-1.0, -1.0, None, -1.0, 0.0, 0.0, -1.0,
876-
None, 0.0, -1.0, -1.0, 0.0, None, 1.0,
877-
0.0, 0.0, 1.0, None, 0.0, 1.0, 1.0, 0.0,
878-
None, 0.0, 1.0, 0.0, 0.0, None, 1.0, 0.0,
879-
1.0, 1.0, None]),
880-
'z': np.array([-0.0, -0.0, 0.0, -0.0, None, -0.0, -0.0,
881-
1.0, -0.0, None, -0.0, 0.0, 0.0, -0.0,
882-
None, 0.0, -0.0, -1.0, 0.0, None, 0.0,
883-
-0.0, 0.0, 0.0, None, -0.0, 0.0, -1.0,
884-
-0.0, None, 0.0, 0.0, 0.0, 0.0, None,
885-
0.0, 0.0, 1.0, 0.0, None])},
861+
'x': [-1.0, 0.0, 0.0, -1.0, None, 0.0, -1.0, -1.0, 0.0,
862+
None, 0.0, 1.0, 0.0, 0.0, None, 1.0, 0.0, 1.0,
863+
1.0, None, 0.0, -1.0, 0.0, 0.0, None, -1.0, 0.0,
864+
-1.0, -1.0, None, 1.0, 0.0, 0.0, 1.0, None, 0.0,
865+
1.0, 1.0, 0.0, None],
866+
'y': [0.0, -1.0, 0.0, 0.0, None, -1.0, 0.0, -1.0, -1.0,
867+
None, -1.0, 0.0, 0.0, -1.0, None, 0.0, -1.0, -1.0,
868+
0.0, None, 1.0, 0.0, 0.0, 1.0, None, 0.0, 1.0,
869+
1.0, 0.0, None, 0.0, 1.0, 0.0, 0.0, None, 1.0,
870+
0.0, 1.0, 1.0, None],
871+
'z': [-0.0, -0.0, 0.0, -0.0, None, -0.0, -0.0, 1.0,
872+
-0.0, None, -0.0, 0.0, 0.0, -0.0, None, 0.0, -0.0,
873+
-1.0, 0.0, None, 0.0, -0.0, 0.0, 0.0, None, -0.0,
874+
0.0, -1.0, -0.0, None, 0.0, 0.0, 0.0, 0.0, None,
875+
0.0, 0.0, 1.0, 0.0, None]},
886876
{'hoverinfo': 'None',
887877
'marker': {'color': [-0.33333333333333331,
888878
0.33333333333333331],
889-
'colorscale': [
890-
[0.0, 'rgb(31, 119, 180)'],
891-
[0.1111111111111111, 'rgb(255, 127, 14)'],
892-
[0.2222222222222222, 'rgb(44, 160, 44)'],
893-
[0.3333333333333333, 'rgb(214, 39, 40)'],
894-
[0.4444444444444444, 'rgb(148, 103, 189)'],
895-
[0.5555555555555556, 'rgb(140, 86, 75)'],
896-
[0.6666666666666666, 'rgb(227, 119, 194)'],
897-
[0.7777777777777778, 'rgb(127, 127, 127)'],
898-
[0.8888888888888888, 'rgb(188, 189, 34)'],
899-
[1.0, 'rgb(23, 190, 207)']
900-
],
879+
'colorscale': [[0.0, 'rgb(31, 119, 180)'],
880+
[1.0, 'rgb(255, 127, 14)']],
901881
'showscale': True,
902882
'size': 0.1},
903883
'mode': 'markers',
904884
'showlegend': False,
905885
'type': 'scatter3d',
906-
'x': [-1.0],
907-
'y': [-1.0],
908-
'z': [1.0]}],
886+
'x': [-1.],
887+
'y': [-1.],
888+
'z': [1.]}],
909889
'layout': {'height': 800,
910890
'scene': {'aspectratio': {'x': 1, 'y': 1, 'z': 1},
911891
'xaxis': {'backgroundcolor': 'rgb(230, 230, 230)',

plotly/tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3683,7 +3683,8 @@ def dist_origin(x, y, z):
36833683
# Validate colormap
36843684
colors.validate_colors(colormap)
36853685
colormap, scale = colors.convert_colors_to_same_type(
3686-
colormap, colortype='tuple', scale=scale
3686+
colormap, colortype='tuple',
3687+
return_default_colors=True, scale=scale
36873688
)
36883689

36893690
data1 = FigureFactory._trisurf(x, y, z, simplices,

0 commit comments

Comments
 (0)