Repository: scipy/scipy

Issue:
BUG: leaves_color_list incorrect when distance=0
### Describe your issue.

The colors in `leaves_color_list` can be wrong when distance is zero.

See: [Previous discussion](https://github.com/scipy/scipy/pull/12450)

FYI, @AtsushiSakai and thank you for your patient discussion about this already.

### Repro code A
```
x = np.array([[1, 0, 0], 
              [0, 0, 1], 
              [0, 2, 0], 
              [0, 0, 1], 
              [0, 1, 0], 
              [0, 1, 0]] )

plt.figure( figsize=( 15, 3 ) )
plt.box( False )
z = linkage( x, "single" )
d = dendrogram( z )

for label, leaf_color in zip( plt.gca().get_xmajorticklabels(), d["leaves_color_list"] ):
    label.set_color( leaf_color )

print( d["leaves_color_list"] )
```

### Output A

You can see that the leaf color for rows ( 4 ) and ( 1 ) are wrong:

<img src="https://user-images.githubusercontent.com/18294329/177059204-4b52e6ee-ba8a-4f8f-a464-908addc63a77.png" alt="Output" width="90%" />

### Repro code B

When I change one value so the distance between row ( 1 ) and row ( 3 ) is slightly more than zero:

```
x = np.array([[1, 0, 0], 
              [0, 0, 1.1], 
              [0, 2, 0], 
              [0, 0, 1], 
              [0, 1, 0], 
              [0, 1, 0]] )

plt.figure( figsize=( 15, 3 ) )
plt.box( False )
z = linkage( x, "single" )
d = dendrogram( z )

for label, leaf_color in zip( plt.gca().get_xmajorticklabels(), d["leaves_color_list"] ):
    label.set_color( leaf_color )

print( d["leaves_color_list"] )
```

### Output

You can see the color for leaf ( 1 ) is correct now:

<img src="https://user-images.githubusercontent.com/18294329/177226416-126e35ae-736c-45ff-af39-268b2101f98f.png" alt="Output" width="90%" />


### Reproducing Code Example

```python
x = np.array([[1, 0, 0], 
              [0, 0, 1], 
              [0, 2, 0], 
              [0, 0, 1], 
              [0, 1, 0], 
              [0, 1, 0]] )

plt.figure( figsize=( 15, 3 ) )
plt.box( False )
z = linkage( x, "single" )
d = dendrogram( z )

for label, leaf_color in zip( plt.gca().get_xmajorticklabels(), d["leaves_color_list"] ):
    label.set_color( leaf_color )

print( d["leaves_color_list"] )
```


### Error message

```shell
None.
```


### SciPy/NumPy/Python version information

1.6.2 1.19.3 sys.version_info(major=3, minor=8, micro=11, releaselevel='final', serial=0)


Discussion:

Resolving patch:
diff --git a/scipy/cluster/hierarchy.py b/scipy/cluster/hierarchy.py
--- a/scipy/cluster/hierarchy.py
+++ b/scipy/cluster/hierarchy.py
@@ -3396,7 +3396,9 @@ def _get_leaves_color_list(R):
                                           R['dcoord'],
                                           R['color_list']):
         for (xi, yi) in zip(link_x, link_y):
-            if yi == 0.0:  # if yi is 0.0, the point is a leaf
+            if yi == 0.0 and (xi % 5 == 0 and xi % 2 == 1):
+                # if yi is 0.0 and xi is divisible by 5 and odd,
+                # the point is a leaf
                 # xi of leaves are      5, 15, 25, 35, ... (see `iv_ticks`)
                 # index of leaves are   0,  1,  2,  3, ... as below
                 leaf_index = (int(xi) - 5) // 10


Test patch:
diff --git a/scipy/cluster/tests/test_hierarchy.py b/scipy/cluster/tests/test_hierarchy.py
--- a/scipy/cluster/tests/test_hierarchy.py
+++ b/scipy/cluster/tests/test_hierarchy.py
@@ -950,6 +950,36 @@ def test_dendrogram_colors(self):
         # reset color palette (global list)
         set_link_color_palette(None)
 
+    def test_dendrogram_leaf_colors_zero_dist(self):
+        # tests that the colors of leafs are correct for tree
+        # with two identical points
+        x = np.array([[1, 0, 0],
+                      [0, 0, 1],
+                      [0, 2, 0],
+                      [0, 0, 1],
+                      [0, 1, 0],
+                      [0, 1, 0]])
+        z = linkage(x, "single")
+        d = dendrogram(z, no_plot=True)
+        exp_colors = ['C0', 'C1', 'C1', 'C0', 'C2', 'C2']
+        colors = d["leaves_color_list"]
+        assert_equal(colors, exp_colors)
+
+    def test_dendrogram_leaf_colors(self):
+        # tests that the colors are correct for a tree
+        # with two near points ((0, 0, 1.1) and (0, 0, 1))
+        x = np.array([[1, 0, 0],
+                      [0, 0, 1.1],
+                      [0, 2, 0],
+                      [0, 0, 1],
+                      [0, 1, 0],
+                      [0, 1, 0]])
+        z = linkage(x, "single")
+        d = dendrogram(z, no_plot=True)
+        exp_colors = ['C0', 'C1', 'C1', 'C0', 'C2', 'C2']
+        colors = d["leaves_color_list"]
+        assert_equal(colors, exp_colors)
+
 
 def calculate_maximum_distances(Z):
     # Used for testing correctness of maxdists.