Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated heatmap figure factory fixes and improvements #1151

Merged
merged 1 commit into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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