# Buggy code
def get_color_dict(self, colorscale):
    """
    Returns colorscale used for dendrogram tree clusters.

    :param (list) colorscale: Colors to use for the plot in rgb format.
    :rtype (dict): A dict of default colors mapped to the user colorscale.

    """

    # These are the color codes returned for dendrograms
    # We're replacing them with nicer colors
    d = {
        "r": "red",
        "g": "green",
        "b": "blue",
        "c": "cyan",
        "m": "magenta",
        "y": "yellow",
        "k": "black",
        "w": "white",
    }
    default_colors = OrderedDict(sorted(d.items(), key=lambda t: t[0]))

    if colorscale is None:
        colorscale = [
            "rgb(0,116,217)",  # blue
            "rgb(35,205,205)",  # cyan
            "rgb(61,153,112)",  # green
            "rgb(40,35,35)",  # black
            "rgb(133,20,75)",  # magenta
            "rgb(255,65,54)",  # red
            "rgb(255,255,255)",  # white
            "rgb(255,220,0)",
        ]  # yellow

    for i in range(len(default_colors.keys())):
        k = list(default_colors.keys())[i]  # PY3 won't index keys
        if i < len(colorscale):
            default_colors[k] = colorscale[i]

    return default_colors


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-386e391a19fb> in <module>
4
5 X = np.random.rand(10,10)
----> 6 fig = ff.create_dendrogram(X)
7 fig.update_layout(width=800, height=500)
8 fig.show()

~/miniconda3/envs/jupyterlab_new/lib/python3.8/site-packages/plotly/figure_factory/_dendrogram.py in create_dendrogram(X, orientation, labels, colorscale, distfun, linkagefun, hovertext, color_threshold)
89         distfun = scs.distance.pdist
90
---> 91     dendrogram = _Dendrogram(
92         X,
93         orientation,

~/miniconda3/envs/jupyterlab_new/lib/python3.8/site-packages/plotly/figure_factory/_dendrogram.py in __init__(self, X, orientation, labels, colorscale, width, height, xaxis, yaxis, distfun, linkagefun, hovertext, color_threshold)
143             distfun = scs.distance.pdist
144
--> 145         (dd_traces, xvals, yvals, ordered_labels, leaves) = self.get_dendrogram_traces(
146             X, colorscale, distfun, linkagefun, hovertext, color_threshold
147         )

~/miniconda3/envs/jupyterlab_new/lib/python3.8/site-packages/plotly/figure_factory/_dendrogram.py in get_dendrogram_traces(self, X, colorscale, distfun, linkagefun, hovertext, color_threshold)
335                 y=np.multiply(self.sign[self.yaxis], ys),
336                 mode="lines",
--> 337                 marker=dict(color=colors[color_key]),
338                 text=hovertext_label,
339                 hoverinfo="text",

KeyError: 'C1'

# Fixed code
def get_color_dict(self, colorscale):
    """
    Returns colorscale used for dendrogram tree clusters.

    :param (list) colorscale: Colors to use for the plot in rgb format.
    :rtype (dict): A dict of default colors mapped to the user colorscale.

    """

    # These are the color codes returned for dendrograms
    # We're replacing them with nicer colors
    # This list is the colors that can be used by dendrogram, which were
    # determined as the combination of the default above_threshold_color and
    # the default color palette (see scipy/cluster/hierarchy.py)
    d = {
        "r": "red",
        "g": "green",
        "b": "blue",
        "c": "cyan",
        "m": "magenta",
        "y": "yellow",
        "k": "black",
        # TODO: 'w' doesn't seem to be in the default color
        # palette in scipy/cluster/hierarchy.py
        "w": "white",
    }
    default_colors = OrderedDict(sorted(d.items(), key=lambda t: t[0]))

    if colorscale is None:
        rgb_colorscale = [
            "rgb(0,116,217)",  # blue
            "rgb(35,205,205)",  # cyan
            "rgb(61,153,112)",  # green
            "rgb(40,35,35)",  # black
            "rgb(133,20,75)",  # magenta
            "rgb(255,65,54)",  # red
            "rgb(255,255,255)",  # white
            "rgb(255,220,0)",  # yellow
        ]
    else:
        rgb_colorscale = colorscale

    for i in range(len(default_colors.keys())):
        k = list(default_colors.keys())[i]  # PY3 won't index keys
        if i < len(rgb_colorscale):
            default_colors[k] = rgb_colorscale[i]

    # add support for cyclic format colors as introduced in scipy===1.5.0
    # before this, the colors were named 'r', 'b', 'y' etc., now they are
    # named 'C0', 'C1', etc. To keep the colors consistent regardless of the
    # scipy version, we try as much as possible to map the new colors to the
    # old colors
    # this mapping was found by inpecting scipy/cluster/hierarchy.py (see
    # comment above).
    new_old_color_map = [
        ("C0", "b"),
        ("C1", "g"),
        ("C2", "r"),
        ("C3", "c"),
        ("C4", "m"),
        ("C5", "y"),
        ("C6", "k"),
        ("C7", "g"),
        ("C8", "r"),
        ("C9", "c"),
    ]
    for nc, oc in new_old_color_map:
        try:
            default_colors[nc] = default_colors[oc]
        except KeyError:
            # it could happen that the old color isn't found (if a custom
            # colorscale was specified), in this case we set it to an
            # arbitrary default.
            default_colors[n] = "rgb(0,116,217)"

    return default_colors