@@ -483,3 +483,49 @@ def test_legend_multiple_sides_with_span():
483483 assert leg_top is not None
484484 assert leg_right is not None
485485 assert leg_left is not None
486+
487+
488+ def test_legend_auto_collect_handles_labels_with_span ():
489+ """Test automatic collection of handles and labels from multiple axes with span parameters."""
490+
491+ fig , axs = uplt .subplots (nrows = 2 , ncols = 2 )
492+
493+ # Create different plots in each subplot with labels
494+ axs [0 , 0 ].plot ([0 , 1 ], [0 , 1 ], label = "line1" )
495+ axs [0 , 1 ].plot ([0 , 1 ], [1 , 0 ], label = "line2" )
496+ axs [1 , 0 ].scatter ([0.5 ], [0.5 ], label = "point1" )
497+ axs [1 , 1 ].scatter ([0.5 ], [0.5 ], label = "point2" )
498+
499+ # Test automatic collection with span parameter (no explicit handles/labels)
500+ leg = fig .legend (ax = axs [0 , :], span = (1 , 2 ), loc = "bottom" )
501+
502+ # Verify legend was created and contains all handles/labels from both axes
503+ assert leg is not None
504+ assert len (leg .get_texts ()) == 2 # Should have 2 labels (line1, line2)
505+
506+ # Test with rows parameter
507+ leg2 = fig .legend (ax = axs [:, 0 ], rows = (1 , 2 ), loc = "right" )
508+ assert leg2 is not None
509+ assert len (leg2 .get_texts ()) == 2 # Should have 2 labels (line1, point1)
510+
511+
512+ def test_legend_explicit_handles_labels_override_auto_collection ():
513+ """Test that explicit handles/labels override auto-collection."""
514+
515+ fig , axs = uplt .subplots (nrows = 1 , ncols = 2 )
516+
517+ # Create plots with labels
518+ (h1 ,) = axs [0 ].plot ([0 , 1 ], [0 , 1 ], label = "auto_label1" )
519+ (h2 ,) = axs [1 ].plot ([0 , 1 ], [1 , 0 ], label = "auto_label2" )
520+
521+ # Test with explicit handles/labels (should override auto-collection)
522+ custom_handles = [h1 ]
523+ custom_labels = ["custom_label" ]
524+ leg = fig .legend (
525+ ax = axs , span = (1 , 2 ), loc = "bottom" , handles = custom_handles , labels = custom_labels
526+ )
527+
528+ # Verify legend uses explicit handles/labels, not auto-collected ones
529+ assert leg is not None
530+ assert len (leg .get_texts ()) == 1
531+ assert leg .get_texts ()[0 ].get_text () == "custom_label"
0 commit comments