@@ -374,6 +374,22 @@ def plot(self, leaf_separation=1, cmap='viridis', select_clusters=False,
374
374
raise ImportError ('You must have matplotlib.patches available to plot selected clusters.' )
375
375
376
376
chosen_clusters = self ._select_clusters ()
377
+
378
+ # Extract the chosen cluster bounds. If enough duplicate data points exist in the
379
+ # data the lambda value might be infinite. This breaks labeling and highlighting
380
+ # the chosen clusters.
381
+ cluster_bounds = np .array ([ plot_data ['cluster_bounds' ][c ] for c in chosen_clusters ])
382
+ if not np .isfinite (cluster_bounds ).all ():
383
+ warn ('Infinite lambda values encountered in chosen clusters.'
384
+ ' This might be due to duplicates in the data.' )
385
+
386
+ # Extract the plot range of the y-axis and set default center and height values for ellipses.
387
+ # Extremly dense clusters might result in near infinite lambda values. Setting max_height
388
+ # based on the percentile should alleviate the impact on plotting.
389
+ plot_range = np .hstack ([plot_data ['bar_tops' ], plot_data ['bar_bottoms' ]])
390
+ plot_range = plot_range [np .isfinite (plot_range )]
391
+ mean_y_center = np .mean ([np .max (plot_range ), np .min (plot_range )])
392
+ max_height = np .diff (np .percentile (plot_range , q = [10 ,90 ]))
377
393
378
394
for i , c in enumerate (chosen_clusters ):
379
395
c_bounds = plot_data ['cluster_bounds' ][c ]
@@ -383,6 +399,17 @@ def plot(self, leaf_separation=1, cmap='viridis', select_clusters=False,
383
399
np .mean ([c_bounds [CB_LEFT ], c_bounds [CB_RIGHT ]]),
384
400
np .mean ([c_bounds [CB_TOP ], c_bounds [CB_BOTTOM ]]),
385
401
)
402
+
403
+ # Set center and height to default values if necessary
404
+ if not np .isfinite (center [1 ]):
405
+ center = (center [0 ], mean_y_center )
406
+ if not np .isfinite (height ):
407
+ height = max_height
408
+
409
+ # Ensure the ellipse is visible
410
+ min_height = 0.1 * max_height
411
+ if height < min_height :
412
+ height = min_height
386
413
387
414
if selection_palette is not None and \
388
415
len (selection_palette ) >= len (chosen_clusters ):
0 commit comments