# Buggy code
def __init__(
    self,
    X,
    orientation="bottom",
    labels=None,
    colorscale=None,
    width=np.inf,
    height=np.inf,
    xaxis="xaxis",
    yaxis="yaxis",
    distfun=None,
    linkagefun=lambda x: sch.linkage(x, "complete"),
    hovertext=None,
    color_threshold=None,
):
    self.orientation = orientation
    self.labels = labels
    self.xaxis = xaxis
    self.yaxis = yaxis
    self.data = []
    self.leaves = []
    self.sign = {self.xaxis: 1, self.yaxis: 1}
    self.layout = {self.xaxis: {}, self.yaxis: {}}

    if self.orientation in ["left", "bottom"]:
        self.sign[self.xaxis] = 1
    else:
        self.sign[self.xaxis] = -1

    if self.orientation in ["right", "bottom"]:
        self.sign[self.yaxis] = 1
    else:
        self.sign[self.yaxis] = -1

    if distfun is None:
        distfun = scs.distance.pdist

    (dd_traces, xvals, yvals, ordered_labels, leaves) = self.get_dendrogram_traces(
        X, colorscale, distfun, linkagefun, hovertext, color_threshold
    )

    self.labels = ordered_labels
    self.leaves = leaves
    yvals_flat = yvals.flatten()
    xvals_flat = xvals.flatten()

    self.zero_vals = []

    for i in range(len(yvals_flat)):
        if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals:
            self.zero_vals.append(xvals_flat[i])

    if len(self.zero_vals) > len(yvals) + 1:
        # If the length of zero_vals is larger than the length of yvals,
        # it means that there are wrong vals because of the identicial samples.
        # Three and more identicial samples will make the yvals of spliting center into 0 and it will \
        # accidentally take it as leaves.
        l_border = int(min(self.zero_vals))
        r_border = int(max(self.zero_vals))
        correct_leaves_pos = range(
            l_border, r_border + 1, int((r_border - l_border) / len(yvals))
        )
        # Regenerating the leaves pos from the self.zero_vals with equally intervals.
        self.zero_vals = [v for v in correct_leaves_pos]

    self.zero_vals.sort()
    self.layout = self.set_figure_layout(width, height)
    self.data = dd_traces


# 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 __init__(
    self,
    X,
    orientation="bottom",
    labels=None,
    colorscale=None,
    width=np.inf,
    height=np.inf,
    xaxis="xaxis",
    yaxis="yaxis",
    distfun=None,
    linkagefun=lambda x: sch.linkage(x, "complete"),
    hovertext=None,
    color_threshold=None,
):
    self.orientation = orientation
    self.labels = labels
    self.xaxis = xaxis
    self.yaxis = yaxis
    self.data = []
    self.leaves = []
    self.sign = {self.xaxis: 1, self.yaxis: 1}
    self.layout = {self.xaxis: {}, self.yaxis: {}}

    if self.orientation in ["left", "bottom"]:
        self.sign[self.xaxis] = 1
    else:
        self.sign[self.xaxis] = -1

    if self.orientation in ["right", "bottom"]:
        self.sign[self.yaxis] = 1
    else:
        self.sign[self.yaxis] = -1

    if distfun is None:
        distfun = scs.distance.pdist

    (dd_traces, xvals, yvals, ordered_labels, leaves) = self.get_dendrogram_traces(
        X, colorscale, distfun, linkagefun, hovertext, color_threshold
    )

    self.labels = ordered_labels
    self.leaves = leaves
    yvals_flat = yvals.flatten()
    xvals_flat = xvals.flatten()

    self.zero_vals = []

    for i in range(len(yvals_flat)):
        if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals:
            self.zero_vals.append(xvals_flat[i])

    if len(self.zero_vals) > len(yvals) + 1:
        # If the length of zero_vals is larger than the length of yvals,
        # it means that there are wrong vals because of the identicial samples.
        # Three and more identicial samples will make the yvals of spliting
        # center into 0 and it will accidentally take it as leaves.
        l_border = int(min(self.zero_vals))
        r_border = int(max(self.zero_vals))
        correct_leaves_pos = range(
            l_border, r_border + 1, int((r_border - l_border) / len(yvals))
        )
        # Regenerating the leaves pos from the self.zero_vals with equally intervals.
        self.zero_vals = [v for v in correct_leaves_pos]

    self.zero_vals.sort()
    self.layout = self.set_figure_layout(width, height)
    self.data = dd_traces