Skip to content

Commit 21b38ab

Browse files
dendrogram works with scipy===1.5.0 default colors
The default colorscale for _dendrogram contains color names compatible with the default colors given by scipy===1.5.0. It is still backwards compatible with older scipy versions.
1 parent d601477 commit 21b38ab

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

Diff for: packages/python/plotly/plotly/figure_factory/_dendrogram.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,45 @@ def get_color_dict(self, colorscale):
198198
default_colors = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
199199

200200
if colorscale is None:
201-
colorscale = [
201+
rgb_colorscale = [
202202
"rgb(0,116,217)", # blue
203203
"rgb(35,205,205)", # cyan
204204
"rgb(61,153,112)", # green
205205
"rgb(40,35,35)", # black
206206
"rgb(133,20,75)", # magenta
207207
"rgb(255,65,54)", # red
208208
"rgb(255,255,255)", # white
209-
"rgb(255,220,0)",
210-
] # yellow
209+
"rgb(255,220,0)", # yellow
210+
]
211+
else:
212+
rgb_colorscale = colorscale
211213

212214
for i in range(len(default_colors.keys())):
213215
k = list(default_colors.keys())[i] # PY3 won't index keys
214-
if i < len(colorscale):
215-
default_colors[k] = colorscale[i]
216+
if i < len(rgb_colorscale):
217+
default_colors[k] = rgb_colorscale[i]
218+
219+
# add support for cyclic format colors as introduced in scipy===1.5.0
220+
# before this, color_list, from which the color_key is obtained was:
221+
# ['g', 'r', 'b', 'c', 'm', 'b', 'b', 'b', 'b'], now it is
222+
# ['C1', 'C2', 'C0', 'C3', 'C4', 'C0', 'C0', 'C0', 'C0'], so to keep the
223+
# colors consistent regardless of the version of scipy, 'C1' is mapped
224+
# to 'rgb(61,153,112)' (what 'g' was mapped to before), 'C2' is mapped
225+
# to 'rgb(255,65,54)', etc.
226+
cyclic_color_names = ["C%d" % (n,) for n in range(5)]
227+
if colorscale is None:
228+
cyclic_color_rgb = [
229+
"rgb(0,116,217)",
230+
"rgb(61,153,112)",
231+
"rgb(255,65,54)",
232+
"rgb(35,205,205)",
233+
"rgb(133,20,75)",
234+
]
235+
else:
236+
cyclic_color_rgb = colorscale
237+
238+
for k, c in zip(cyclic_color_names, cyclic_color_rgb):
239+
default_colors[k] = c
216240

217241
return default_colors
218242

0 commit comments

Comments
 (0)