@@ -64,6 +64,7 @@ def create_annotated_heatmap(z, x=None, y=None, annotation_text=None,
64
64
defined, the colors are defined logically as black or white
65
65
depending on the heatmap's colorscale.
66
66
:param (bool) showscale: Display colorscale. Default = False
67
+ :param (bool) reversescale: Reverse colorscale. Default = False
67
68
:param kwargs: kwargs passed through plotly.graph_objs.Heatmap.
68
69
These kwargs describe other attributes about the annotated Heatmap
69
70
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,
98
99
99
100
if x or y :
100
101
trace = dict (type = 'heatmap' , z = z , x = x , y = y , colorscale = colorscale ,
101
- showscale = showscale , ** kwargs )
102
+ showscale = showscale , reversescale = reversescale , ** kwargs )
102
103
layout = dict (annotations = annotations ,
103
104
xaxis = dict (ticks = '' , dtick = 1 , side = 'top' ,
104
105
gridcolor = 'rgb(0, 0, 0)' ),
105
106
yaxis = dict (ticks = '' , dtick = 1 , ticksuffix = ' ' ))
106
107
else :
107
108
trace = dict (type = 'heatmap' , z = z , colorscale = colorscale ,
108
- showscale = showscale , ** kwargs )
109
+ showscale = showscale , reversescale = reversescale , ** kwargs )
109
110
layout = dict (annotations = annotations ,
110
111
xaxis = dict (ticks = '' , side = 'top' ,
111
112
gridcolor = 'rgb(0, 0, 0)' ,
@@ -127,6 +128,12 @@ def to_rgb_color_list(color_str, default):
127
128
return default
128
129
129
130
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
+
130
137
class _AnnotatedHeatmap (object ):
131
138
"""
132
139
Refer to TraceFactory.create_annotated_heatmap() for docstring
@@ -173,39 +180,47 @@ def get_text_color(self):
173
180
'Earth' , 'Electric' , 'Viridis' , 'Cividis' ]
174
181
# Plotly colorscales ranging from a darker shade to a lighter shade
175
182
colorscales_reverse = ['Reds' ]
183
+
184
+ white = '#FFFFFF'
185
+ black = '#000000'
176
186
if self .font_colors :
177
187
min_text_color = self .font_colors [0 ]
178
188
max_text_color = self .font_colors [- 1 ]
179
189
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
182
192
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
185
195
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
188
198
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
191
201
elif isinstance (self .colorscale , list ):
192
202
193
203
min_col = to_rgb_color_list (self .colorscale [0 ][1 ],
194
204
[255 , 255 , 255 ])
195
205
max_col = to_rgb_color_list (self .colorscale [- 1 ][1 ],
196
206
[255 , 255 , 255 ])
197
207
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
200
214
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
204
219
else :
205
- max_text_color = '#FFFFFF'
220
+ max_text_color = white
206
221
else :
207
- min_text_color = '#000000'
208
- max_text_color = '#000000'
222
+ min_text_color = black
223
+ max_text_color = black
209
224
return min_text_color , max_text_color
210
225
211
226
def get_z_mid (self ):
0 commit comments