Skip to content

Commit

Permalink
Annotated heatmap figure factory fixes and improvements (#1151)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jonmmease authored Sep 2, 2018
1 parent 121dd66 commit 96ade26
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
33 changes: 21 additions & 12 deletions plotly/figure_factory/_annotated_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from plotly import exceptions, optional_imports
from plotly.figure_factory import utils
from plotly.graph_objs import graph_objs
from plotly.validators.heatmap import ColorscaleValidator

# Optional imports, may be None for users that only use our core functionality.
np = optional_imports.get_module('numpy')
Expand Down Expand Up @@ -86,6 +87,11 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
# Avoiding mutables in the call signature
font_colors = font_colors if font_colors is not None else []
validate_annotated_heatmap(z, x, y, annotation_text)

# validate colorscale
colorscale_validator = ColorscaleValidator()
colorscale = colorscale_validator.validate_coerce(colorscale)

annotations = _AnnotatedHeatmap(z, x, y, annotation_text,
colorscale, font_colors, reversescale,
**kwargs).make_annotations()
Expand All @@ -112,6 +118,15 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
return graph_objs.Figure(data=data, layout=layout)


def to_rgb_color_list(color_str, default):
if 'rgb' in color_str:
return [int(v) for v in color_str.strip('rgb()').split(',')]
elif '#' in color_str:
return utils.hex_to_rgb(color_str)
else:
return default


class _AnnotatedHeatmap(object):
"""
Refer to TraceFactory.create_annotated_heatmap() for docstring
Expand Down Expand Up @@ -155,7 +170,7 @@ def get_text_color(self):
colorscales = ['Greys', 'Greens', 'Blues',
'YIGnBu', 'YIOrRd', 'RdBu',
'Picnic', 'Jet', 'Hot', 'Blackbody',
'Earth', 'Electric', 'Viridis']
'Earth', 'Electric', 'Viridis', 'Cividis']
# Plotly colorscales ranging from a darker shade to a lighter shade
colorscales_reverse = ['Reds']
if self.font_colors:
Expand All @@ -174,17 +189,11 @@ def get_text_color(self):
min_text_color = '#000000'
max_text_color = '#FFFFFF'
elif isinstance(self.colorscale, list):
if 'rgb' in self.colorscale[0][1]:
min_col = map(int,
self.colorscale[0][1].strip('rgb()').split(','))
max_col = map(int,
self.colorscale[-1][1].strip('rgb()').split(','))
elif '#' in self.colorscale[0][1]:
min_col = utils.hex_to_rgb(self.colorscale[0][1])
max_col = utils.hex_to_rgb(self.colorscale[-1][1])
else:
min_col = [255, 255, 255]
max_col = [255, 255, 255]

min_col = to_rgb_color_list(self.colorscale[0][1],
[255, 255, 255])
max_col = to_rgb_color_list(self.colorscale[-1][1],
[255, 255, 255])

if (min_col[0]*0.299 + min_col[1]*0.587 + min_col[2]*0.114) > 186:
min_text_color = '#000000'
Expand Down
18 changes: 8 additions & 10 deletions plotly/tests/test_optional/test_tools/test_figure_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,16 +822,14 @@ def test_annotated_heatmap_kwargs(self):

z = [[1, 0], [.25, .75], [.45, .5]]
text = [['first', 'second'], ['third', 'fourth'], ['fifth', 'sixth']]
a = ff.create_annotated_heatmap(z, x=['A', 'B'],
y=['One', 'Two',
'Three'],
annotation_text=text,
colorscale=[[0,
'#ffffff'],
[1,
'#e6005a']]
)
expected_a = {'data': [{'colorscale': [[0, '#ffffff'], [1, '#e6005a']],
a = ff.create_annotated_heatmap(z,
x=['A', 'B'],
y=['One', 'Two', 'Three'],
annotation_text=text,
colorscale=[[0, 'rgb(255,255,255)'],
[1, '#e6005a']])
expected_a = {'data': [{'colorscale':
[[0, 'rgb(255,255,255)'], [1, '#e6005a']],
'showscale': False,
'type': 'heatmap',
'x': ['A', 'B'],
Expand Down

0 comments on commit 96ade26

Please sign in to comment.