@@ -64,6 +64,7 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
6464 defined, the colors are defined logically as black or white
6565 depending on the heatmap's colorscale.
6666 :param (bool) showscale: Display colorscale. Default = False
67+ :param (bool) reversescale: Reverse colorscale. Default = False
6768 :param kwargs: kwargs passed through plotly.graph_objs.Heatmap.
6869 These kwargs describe other attributes about the annotated Heatmap
6970 trace such as the colorscale. For more information on valid kwargs
@@ -98,14 +99,14 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
9899
99100 if x or y :
100101 trace = dict (type = 'heatmap' , z = z , x = x , y = y , colorscale = colorscale ,
101- showscale = showscale , ** kwargs )
102+ showscale = showscale , reversescale = reversescale , ** kwargs )
102103 layout = dict (annotations = annotations ,
103104 xaxis = dict (ticks = '' , dtick = 1 , side = 'top' ,
104105 gridcolor = 'rgb(0, 0, 0)' ),
105106 yaxis = dict (ticks = '' , dtick = 1 , ticksuffix = ' ' ))
106107 else :
107108 trace = dict (type = 'heatmap' , z = z , colorscale = colorscale ,
108- showscale = showscale , ** kwargs )
109+ showscale = showscale , reversescale = reversescale , ** kwargs )
109110 layout = dict (annotations = annotations ,
110111 xaxis = dict (ticks = '' , side = 'top' ,
111112 gridcolor = 'rgb(0, 0, 0)' ,
@@ -127,6 +128,12 @@ def to_rgb_color_list(color_str, default):
127128 return default
128129
129130
131+ def should_use_black_text (background_color ):
132+ return (background_color [0 ] * 0.299 +
133+ background_color [1 ] * 0.587 +
134+ background_color [2 ] * 0.114 ) > 186
135+
136+
130137class _AnnotatedHeatmap (object ):
131138 """
132139 Refer to TraceFactory.create_annotated_heatmap() for docstring
@@ -173,39 +180,47 @@ def get_text_color(self):
173180 'Earth' , 'Electric' , 'Viridis' , 'Cividis' ]
174181 # Plotly colorscales ranging from a darker shade to a lighter shade
175182 colorscales_reverse = ['Reds' ]
183+
184+ white = '#FFFFFF'
185+ black = '#000000'
176186 if self .font_colors :
177187 min_text_color = self .font_colors [0 ]
178188 max_text_color = self .font_colors [- 1 ]
179189 elif self .colorscale in colorscales and self .reversescale :
180- min_text_color = '#000000'
181- max_text_color = '#FFFFFF'
190+ min_text_color = black
191+ max_text_color = white
182192 elif self .colorscale in colorscales :
183- min_text_color = '#FFFFFF'
184- max_text_color = '#000000'
193+ min_text_color = white
194+ max_text_color = black
185195 elif self .colorscale in colorscales_reverse and self .reversescale :
186- min_text_color = '#FFFFFF'
187- max_text_color = '#000000'
196+ min_text_color = white
197+ max_text_color = black
188198 elif self .colorscale in colorscales_reverse :
189- min_text_color = '#000000'
190- max_text_color = '#FFFFFF'
199+ min_text_color = black
200+ max_text_color = white
191201 elif isinstance (self .colorscale , list ):
192202
193203 min_col = to_rgb_color_list (self .colorscale [0 ][1 ],
194204 [255 , 255 , 255 ])
195205 max_col = to_rgb_color_list (self .colorscale [- 1 ][1 ],
196206 [255 , 255 , 255 ])
197207
198- if (min_col [0 ]* 0.299 + min_col [1 ]* 0.587 + min_col [2 ]* 0.114 ) > 186 :
199- min_text_color = '#000000'
208+ # swap min/max colors if reverse scale
209+ if self .reversescale :
210+ min_col , max_col = max_col , min_col
211+
212+ if should_use_black_text (min_col ):
213+ min_text_color = black
200214 else :
201- min_text_color = '#FFFFFF'
202- if (max_col [0 ]* 0.299 + max_col [1 ]* 0.587 + max_col [2 ]* 0.114 ) > 186 :
203- max_text_color = '#000000'
215+ min_text_color = white
216+
217+ if should_use_black_text (max_col ):
218+ max_text_color = black
204219 else :
205- max_text_color = '#FFFFFF'
220+ max_text_color = white
206221 else :
207- min_text_color = '#000000'
208- max_text_color = '#000000'
222+ min_text_color = black
223+ max_text_color = black
209224 return min_text_color , max_text_color
210225
211226 def get_z_mid (self ):
0 commit comments