3
3
from plotly import exceptions , optional_imports
4
4
from plotly .figure_factory import utils
5
5
from plotly .graph_objs import graph_objs
6
+ from plotly .validators .heatmap import ColorscaleValidator
6
7
7
8
# Optional imports, may be None for users that only use our core functionality.
8
9
np = optional_imports .get_module ('numpy' )
@@ -86,6 +87,11 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
86
87
# Avoiding mutables in the call signature
87
88
font_colors = font_colors if font_colors is not None else []
88
89
validate_annotated_heatmap (z , x , y , annotation_text )
90
+
91
+ # validate colorscale
92
+ colorscale_validator = ColorscaleValidator ()
93
+ colorscale = colorscale_validator .validate_coerce (colorscale )
94
+
89
95
annotations = _AnnotatedHeatmap (z , x , y , annotation_text ,
90
96
colorscale , font_colors , reversescale ,
91
97
** kwargs ).make_annotations ()
@@ -112,6 +118,15 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
112
118
return graph_objs .Figure (data = data , layout = layout )
113
119
114
120
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
+
115
130
class _AnnotatedHeatmap (object ):
116
131
"""
117
132
Refer to TraceFactory.create_annotated_heatmap() for docstring
@@ -155,7 +170,7 @@ def get_text_color(self):
155
170
colorscales = ['Greys' , 'Greens' , 'Blues' ,
156
171
'YIGnBu' , 'YIOrRd' , 'RdBu' ,
157
172
'Picnic' , 'Jet' , 'Hot' , 'Blackbody' ,
158
- 'Earth' , 'Electric' , 'Viridis' ]
173
+ 'Earth' , 'Electric' , 'Viridis' , 'Cividis' ]
159
174
# Plotly colorscales ranging from a darker shade to a lighter shade
160
175
colorscales_reverse = ['Reds' ]
161
176
if self .font_colors :
@@ -174,17 +189,11 @@ def get_text_color(self):
174
189
min_text_color = '#000000'
175
190
max_text_color = '#FFFFFF'
176
191
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 ])
188
197
189
198
if (min_col [0 ]* 0.299 + min_col [1 ]* 0.587 + min_col [2 ]* 0.114 ) > 186 :
190
199
min_text_color = '#000000'
0 commit comments