Skip to content

Commit 303385d

Browse files
committed
Replaced new color_parser function into scatterplot_theme
1 parent d319448 commit 303385d

File tree

2 files changed

+39
-184
lines changed

2 files changed

+39
-184
lines changed

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,19 +1052,8 @@ def test_valid_colormap(self):
10521052
tls.FigureFactory.create_scatterplotmatrix,
10531053
df, index='a', colormap='fake_scale')
10541054

1055-
pattern = (
1056-
"You must input a valid colormap. Valid types include a plotly "
1057-
"scale, rgb, hex or tuple color, a list of any color types, or a "
1058-
"dictionary with index names each assigned to a color."
1059-
)
1060-
1061-
# check: accepted data type for colormap
1062-
self.assertRaisesRegexp(PlotlyError, pattern,
1063-
tls.FigureFactory.create_scatterplotmatrix,
1064-
df, colormap=1)
1065-
10661055
pattern_rgb = (
1067-
"Whoops! The elements in your rgb colormap tuples cannot "
1056+
"Whoops! The elements in your rgb colors tuples cannot "
10681057
"exceed 255.0."
10691058
)
10701059

@@ -1078,7 +1067,7 @@ def test_valid_colormap(self):
10781067
df, colormap=['rgb(500, 1, 1)'], index='c')
10791068

10801069
pattern_tuple = (
1081-
"Whoops! The elements in your colormap tuples cannot "
1070+
"Whoops! The elements in your colors tuples cannot "
10821071
"exceed 1.0."
10831072
)
10841073

@@ -1243,13 +1232,13 @@ def test_scatter_plot_matrix_kwargs(self):
12431232
)
12441233

12451234
exp_scatter_plot_matrix = {
1246-
'data': [{'marker': {'color': 'rgb(128,0,38)'},
1235+
'data': [{'marker': {'color': 'rgb(128.0, 0.0, 38.0)'},
12471236
'showlegend': False,
12481237
'type': 'histogram',
12491238
'x': [2, -15, -2, 0],
12501239
'xaxis': 'x1',
12511240
'yaxis': 'y1'},
1252-
{'marker': {'color': 'rgb(255,255,204)'},
1241+
{'marker': {'color': 'rgb(255.0, 255.0, 204.0)'},
12531242
'showlegend': False,
12541243
'type': 'histogram',
12551244
'x': [6, 5],

plotly/tools.py

Lines changed: 35 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,13 +1482,11 @@ def _validate_colors(colors, colortype='tuple'):
14821482
from numbers import Number
14831483
if colors is None:
14841484
colors = DEFAULT_PLOTLY_COLORS
1485-
#colors = [DEFAULT_PLOTLY_COLORS[0],
1486-
# DEFAULT_PLOTLY_COLORS[1]]
14871485

14881486
if isinstance(colors, str):
14891487
if colors in PLOTLY_SCALES:
14901488
colors = PLOTLY_SCALES[colors]
1491-
if 'rgb' in colors or '#' in colors:
1489+
elif 'rgb' in colors or '#' in colors:
14921490
colors = [colors]
14931491
else:
14941492
raise exceptions.PlotlyError(
@@ -3004,11 +3002,16 @@ def _scatterplot_theme(dataframe, headers, diag, size, height,
30043002

30053003
# Convert colormap to list of n RGB tuples
30063004
if colormap_type == 'seq':
3007-
foo = FigureFactory._unlabel_rgb(colormap)
3005+
foo = FigureFactory._color_parser(
3006+
colormap, FigureFactory._unlabel_rgb
3007+
)
30083008
foo = FigureFactory._n_colors(foo[0],
30093009
foo[1],
30103010
n_colors_len)
3011-
theme = FigureFactory._label_rgb(foo)
3011+
theme = FigureFactory._color_parser(
3012+
foo, FigureFactory._label_rgb
3013+
)
3014+
30123015
if colormap_type == 'cat':
30133016
# leave list of colors the same way
30143017
theme = colormap
@@ -3173,11 +3176,22 @@ def _scatterplot_theme(dataframe, headers, diag, size, height,
31733176

31743177
# Convert colormap to list of n RGB tuples
31753178
if colormap_type == 'seq':
3176-
foo = FigureFactory._unlabel_rgb(colormap)
3179+
foo = FigureFactory._color_parser(
3180+
colormap, FigureFactory._unlabel_rgb
3181+
)
31773182
foo = FigureFactory._n_colors(foo[0],
31783183
foo[1],
31793184
len(intervals))
3180-
theme = FigureFactory._label_rgb(foo)
3185+
theme = FigureFactory._color_parser(
3186+
foo, FigureFactory._label_rgb
3187+
)
3188+
3189+
3190+
#foo = FigureFactory._unlabel_rgb(colormap)
3191+
#foo = FigureFactory._n_colors(foo[0],
3192+
# foo[1],
3193+
# len(intervals))
3194+
#theme = FigureFactory._label_rgb(foo)
31813195
if colormap_type == 'cat':
31823196
# leave list of colors the same way
31833197
theme = colormap
@@ -3908,154 +3922,16 @@ def create_scatterplotmatrix(df, index=None, endpts=None, diag='scatter',
39083922
headers = []
39093923
if index_vals is None:
39103924
index_vals = []
3911-
plotly_scales = {'Greys': ['rgb(0,0,0)', 'rgb(255,255,255)'],
3912-
'YlGnBu': ['rgb(8,29,88)', 'rgb(255,255,217)'],
3913-
'Greens': ['rgb(0,68,27)', 'rgb(247,252,245)'],
3914-
'YlOrRd': ['rgb(128,0,38)', 'rgb(255,255,204)'],
3915-
'Bluered': ['rgb(0,0,255)', 'rgb(255,0,0)'],
3916-
'RdBu': ['rgb(5,10,172)', 'rgb(178,10,28)'],
3917-
'Reds': ['rgb(220,220,220)', 'rgb(178,10,28)'],
3918-
'Blues': ['rgb(5,10,172)', 'rgb(220,220,220)'],
3919-
'Picnic': ['rgb(0,0,255)', 'rgb(255,0,0)'],
3920-
'Rainbow': ['rgb(150,0,90)', 'rgb(255,0,0)'],
3921-
'Portland': ['rgb(12,51,131)', 'rgb(217,30,30)'],
3922-
'Jet': ['rgb(0,0,131)', 'rgb(128,0,0)'],
3923-
'Hot': ['rgb(0,0,0)', 'rgb(255,255,255)'],
3924-
'Blackbody': ['rgb(0,0,0)', 'rgb(160,200,255)'],
3925-
'Earth': ['rgb(0,0,130)', 'rgb(255,255,255)'],
3926-
'Electric': ['rgb(0,0,0)', 'rgb(255,250,220)'],
3927-
'Viridis': ['rgb(68,1,84)', 'rgb(253,231,37)']}
39283925

39293926
FigureFactory._validate_scatterplotmatrix(df, index, diag,
39303927
colormap_type, **kwargs)
39313928

39323929
# Validate colormap
3933-
if colormap is None:
3934-
colormap = DEFAULT_PLOTLY_COLORS
3935-
3936-
if isinstance(colormap, str):
3937-
if colormap in plotly_scales:
3938-
colormap = plotly_scales[colormap]
3939-
3940-
elif 'rgb' in colormap:
3941-
colormap = FigureFactory._unlabel_rgb(colormap)
3942-
for value in colormap:
3943-
if value > 255.0:
3944-
raise exceptions.PlotlyError("Whoops! The "
3945-
"elements in your "
3946-
"rgb colormap "
3947-
"tuples cannot "
3948-
"exceed 255.0.")
3949-
colormap = FigureFactory._label_rgb(colormap)
3950-
3951-
# put colormap in list
3952-
colors_list = []
3953-
colors_list.append(colormap)
3954-
colormap = colors_list
3955-
3956-
elif '#' in colormap:
3957-
colormap = FigureFactory._hex_to_rgb(colormap)
3958-
colormap = FigureFactory._label_rgb(colormap)
3959-
3960-
# put colormap in list
3961-
colors_list = []
3962-
colors_list.append(colormap)
3963-
colormap = colors_list
3964-
3965-
else:
3966-
scale_keys = list(plotly_scales.keys())
3967-
raise exceptions.PlotlyError("If you input a string "
3968-
"for 'colormap', it must "
3969-
"either be a Plotly "
3970-
"colorscale, an 'rgb' "
3971-
"color or a hex color."
3972-
"Valid plotly colorscale "
3973-
"names are {}".format(scale_keys))
3974-
elif isinstance(colormap, tuple):
3975-
for value in colormap:
3976-
if value > 1.0:
3977-
raise exceptions.PlotlyError("Whoops! The "
3978-
"elements in "
3979-
"your colormap "
3980-
"tuples cannot "
3981-
"exceed 1.0.")
3982-
3983-
colors_list = []
3984-
colors_list.append(colormap)
3985-
colormap = colors_list
3986-
3987-
colormap = FigureFactory._convert_to_RGB_255(colormap)
3988-
colormap = FigureFactory._label_rgb(colormap)
3989-
3990-
elif isinstance(colormap, list):
3991-
new_colormap = []
3992-
for color in colormap:
3993-
if 'rgb' in color:
3994-
color = FigureFactory._unlabel_rgb(color)
3995-
3996-
for value in color:
3997-
if value > 255.0:
3998-
raise exceptions.PlotlyError("Whoops! The "
3999-
"elements in your "
4000-
"rgb colormap "
4001-
"tuples cannot "
4002-
"exceed 255.0.")
4003-
4004-
color = FigureFactory._label_rgb(color)
4005-
new_colormap.append(color)
4006-
elif '#' in color:
4007-
color = FigureFactory._hex_to_rgb(color)
4008-
color = FigureFactory._label_rgb(color)
4009-
new_colormap.append(color)
4010-
elif isinstance(color, tuple):
4011-
for value in color:
4012-
if value > 1.0:
4013-
raise exceptions.PlotlyError("Whoops! The "
4014-
"elements in "
4015-
"your colormap "
4016-
"tuples cannot "
4017-
"exceed 1.0.")
4018-
color = FigureFactory._convert_to_RGB_255(color)
4019-
color = FigureFactory._label_rgb(color)
4020-
new_colormap.append(color)
4021-
colormap = new_colormap
4022-
4023-
elif isinstance(colormap, dict):
4024-
for name in colormap:
4025-
if 'rgb' in colormap[name]:
4026-
color = FigureFactory._unlabel_rgb(colormap[name])
4027-
for value in color:
4028-
if value > 255.0:
4029-
raise exceptions.PlotlyError("Whoops! The "
4030-
"elements in your "
4031-
"rgb colormap "
4032-
"tuples cannot "
4033-
"exceed 255.0.")
4034-
4035-
elif '#' in colormap[name]:
4036-
color = FigureFactory._hex_to_rgb(colormap[name])
4037-
color = FigureFactory._label_rgb(color)
4038-
colormap[name] = color
4039-
4040-
elif isinstance(colormap[name], tuple):
4041-
for value in colormap[name]:
4042-
if value > 1.0:
4043-
raise exceptions.PlotlyError("Whoops! The "
4044-
"elements in "
4045-
"your colormap "
4046-
"tuples cannot "
4047-
"exceed 1.0.")
4048-
color = FigureFactory._convert_to_RGB_255(colormap[name])
4049-
color = FigureFactory._label_rgb(color)
4050-
colormap[name] = color
4051-
3930+
if isinstance(colormap, dict):
3931+
colormap = FigureFactory._validate_colors_dict(colormap, 'rgb')
40523932
else:
4053-
raise exceptions.PlotlyError("You must input a valid colormap. "
4054-
"Valid types include a plotly scale, "
4055-
"rgb, hex or tuple color, a list of "
4056-
"any color types, or a dictionary "
4057-
"with index names each assigned "
4058-
"to a color.")
3933+
colormap = FigureFactory._validate_colors(colormap, 'rgb')
3934+
40593935
if not index:
40603936
for name in df:
40613937
headers.append(name)
@@ -4094,29 +3970,19 @@ def create_scatterplotmatrix(df, index=None, endpts=None, diag='scatter',
40943970
"dictionary, all the "
40953971
"names in the index "
40963972
"must be keys.")
4097-
4098-
figure = FigureFactory._scatterplot_dict(dataframe,
4099-
headers,
4100-
diag,
4101-
size, height,
4102-
width, title,
4103-
index,
4104-
index_vals,
4105-
endpts,
4106-
colormap,
4107-
colormap_type,
4108-
**kwargs)
3973+
figure = FigureFactory._scatterplot_dict(
3974+
dataframe, headers, diag, size, height, width, title,
3975+
index, index_vals, endpts, colormap, colormap_type,
3976+
**kwargs
3977+
)
41093978
return figure
41103979

41113980
else:
4112-
figure = FigureFactory._scatterplot_theme(dataframe, headers,
4113-
diag, size,
4114-
height, width,
4115-
title, index,
4116-
index_vals,
4117-
endpts, colormap,
4118-
colormap_type,
4119-
**kwargs)
3981+
figure = FigureFactory._scatterplot_theme(
3982+
dataframe, headers, diag, size, height, width, title,
3983+
index, index_vals, endpts, colormap, colormap_type,
3984+
**kwargs
3985+
)
41203986
return figure
41213987

41223988
@staticmethod

0 commit comments

Comments
 (0)