Skip to content

Commit 96ade26

Browse files
authored
Annotated heatmap figure factory fixes and improvements (#1151)
- Fix `map object not subscriptable` error (This happened on Python 3 when specifying a custom colormap using `rgb` colors) - Use colorscale validator to validate specified colorscale before trying to do text color inference. - Handle case where some colors are specified as rgb and some as hex. - Add 'Cividis" to list of know lighter-to-darker colorscales.
1 parent 121dd66 commit 96ade26

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

Diff for: plotly/figure_factory/_annotated_heatmap.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from plotly import exceptions, optional_imports
44
from plotly.figure_factory import utils
55
from plotly.graph_objs import graph_objs
6+
from plotly.validators.heatmap import ColorscaleValidator
67

78
# Optional imports, may be None for users that only use our core functionality.
89
np = optional_imports.get_module('numpy')
@@ -86,6 +87,11 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
8687
# Avoiding mutables in the call signature
8788
font_colors = font_colors if font_colors is not None else []
8889
validate_annotated_heatmap(z, x, y, annotation_text)
90+
91+
# validate colorscale
92+
colorscale_validator = ColorscaleValidator()
93+
colorscale = colorscale_validator.validate_coerce(colorscale)
94+
8995
annotations = _AnnotatedHeatmap(z, x, y, annotation_text,
9096
colorscale, font_colors, reversescale,
9197
**kwargs).make_annotations()
@@ -112,6 +118,15 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
112118
return graph_objs.Figure(data=data, layout=layout)
113119

114120

121+
def to_rgb_color_list(color_str, default):
122+
if 'rgb' in color_str:
123+
return [int(v) for v in color_str.strip('rgb()').split(',')]
124+
elif '#' in color_str:
125+
return utils.hex_to_rgb(color_str)
126+
else:
127+
return default
128+
129+
115130
class _AnnotatedHeatmap(object):
116131
"""
117132
Refer to TraceFactory.create_annotated_heatmap() for docstring
@@ -155,7 +170,7 @@ def get_text_color(self):
155170
colorscales = ['Greys', 'Greens', 'Blues',
156171
'YIGnBu', 'YIOrRd', 'RdBu',
157172
'Picnic', 'Jet', 'Hot', 'Blackbody',
158-
'Earth', 'Electric', 'Viridis']
173+
'Earth', 'Electric', 'Viridis', 'Cividis']
159174
# Plotly colorscales ranging from a darker shade to a lighter shade
160175
colorscales_reverse = ['Reds']
161176
if self.font_colors:
@@ -174,17 +189,11 @@ def get_text_color(self):
174189
min_text_color = '#000000'
175190
max_text_color = '#FFFFFF'
176191
elif isinstance(self.colorscale, list):
177-
if 'rgb' in self.colorscale[0][1]:
178-
min_col = map(int,
179-
self.colorscale[0][1].strip('rgb()').split(','))
180-
max_col = map(int,
181-
self.colorscale[-1][1].strip('rgb()').split(','))
182-
elif '#' in self.colorscale[0][1]:
183-
min_col = utils.hex_to_rgb(self.colorscale[0][1])
184-
max_col = utils.hex_to_rgb(self.colorscale[-1][1])
185-
else:
186-
min_col = [255, 255, 255]
187-
max_col = [255, 255, 255]
192+
193+
min_col = to_rgb_color_list(self.colorscale[0][1],
194+
[255, 255, 255])
195+
max_col = to_rgb_color_list(self.colorscale[-1][1],
196+
[255, 255, 255])
188197

189198
if (min_col[0]*0.299 + min_col[1]*0.587 + min_col[2]*0.114) > 186:
190199
min_text_color = '#000000'

Diff for: plotly/tests/test_optional/test_tools/test_figure_factory.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -822,16 +822,14 @@ def test_annotated_heatmap_kwargs(self):
822822

823823
z = [[1, 0], [.25, .75], [.45, .5]]
824824
text = [['first', 'second'], ['third', 'fourth'], ['fifth', 'sixth']]
825-
a = ff.create_annotated_heatmap(z, x=['A', 'B'],
826-
y=['One', 'Two',
827-
'Three'],
828-
annotation_text=text,
829-
colorscale=[[0,
830-
'#ffffff'],
831-
[1,
832-
'#e6005a']]
833-
)
834-
expected_a = {'data': [{'colorscale': [[0, '#ffffff'], [1, '#e6005a']],
825+
a = ff.create_annotated_heatmap(z,
826+
x=['A', 'B'],
827+
y=['One', 'Two', 'Three'],
828+
annotation_text=text,
829+
colorscale=[[0, 'rgb(255,255,255)'],
830+
[1, '#e6005a']])
831+
expected_a = {'data': [{'colorscale':
832+
[[0, 'rgb(255,255,255)'], [1, '#e6005a']],
835833
'showscale': False,
836834
'type': 'heatmap',
837835
'x': ['A', 'B'],

0 commit comments

Comments
 (0)