@@ -433,3 +433,98 @@ def test_dictionary_color(self):
433
433
ax = df1 .plot (kind = "line" , color = dic_color )
434
434
colors = [rect .get_color () for rect in ax .get_lines ()[0 :2 ]]
435
435
assert all (color == expected [index ] for index , color in enumerate (colors ))
436
+
437
+ @pytest .mark .slow
438
+ def test_has_externally_shared_axis (self ):
439
+ func = plotting ._matplotlib .tools ._has_externally_shared_axis
440
+
441
+ # Test x-axis
442
+ fig = self .plt .figure ()
443
+ plots = fig .subplots (2 , 4 )
444
+
445
+ # Create *externally* shared axes for first and third columns
446
+ plots [0 ][0 ] = fig .add_subplot (231 , sharex = plots [1 ][0 ])
447
+ plots [0 ][2 ] = fig .add_subplot (233 , sharex = plots [1 ][2 ])
448
+
449
+ # Create *internally* shared axes for second and third columns
450
+ plots [0 ][1 ].twinx ()
451
+ plots [0 ][2 ].twinx ()
452
+
453
+ # First column is only externally shared
454
+ # Second column is only internally shared
455
+ # Third column is both
456
+ # Fourth column is neither
457
+ assert func (plots [0 ][0 ], "x" )
458
+ assert not func (plots [0 ][1 ], "x" )
459
+ assert func (plots [0 ][2 ], "x" )
460
+ assert not func (plots [0 ][3 ], "x" )
461
+
462
+ # Test y-axis
463
+ fig = self .plt .figure ()
464
+ plots = fig .subplots (4 , 2 )
465
+
466
+ # Create *externally* shared axes for first and third rows
467
+ plots [0 ][0 ] = fig .add_subplot (321 , sharey = plots [0 ][1 ])
468
+ plots [2 ][0 ] = fig .add_subplot (325 , sharey = plots [2 ][1 ])
469
+
470
+ # Create *internally* shared axes for second and third rows
471
+ plots [1 ][0 ].twiny ()
472
+ plots [2 ][0 ].twiny ()
473
+
474
+ # First row is only externally shared
475
+ # Second row is only internally shared
476
+ # Third row is both
477
+ # Fourth row is neither
478
+ assert func (plots [0 ][0 ], "y" )
479
+ assert not func (plots [1 ][0 ], "y" )
480
+ assert func (plots [2 ][0 ], "y" )
481
+ assert not func (plots [3 ][0 ], "y" )
482
+
483
+ # Check that an invalid compare_axis value triggers the expected exception
484
+ msg = "needs 'x' or 'y' as a second parameter"
485
+ with pytest .raises (ValueError , match = msg ):
486
+ func (plots [0 ][0 ], "z" )
487
+
488
+ @pytest .mark .slow
489
+ def test_externally_shared_axes (self ):
490
+ # Example from #33819
491
+ # Create data
492
+ df = DataFrame ({"a" : np .random .randn (1000 ), "b" : np .random .randn (1000 )})
493
+
494
+ # Create figure
495
+ fig = self .plt .figure ()
496
+ plots = fig .subplots (2 , 3 )
497
+
498
+ # Create *externally* shared axes
499
+ plots [0 ][0 ] = fig .add_subplot (231 , sharex = plots [1 ][0 ])
500
+ # note: no plots[0][1] that's the twin only case
501
+ plots [0 ][2 ] = fig .add_subplot (233 , sharex = plots [1 ][2 ])
502
+
503
+ # Create *internally* shared axes
504
+ # note: no plots[0][0] that's the external only case
505
+ twin_ax1 = plots [0 ][1 ].twinx ()
506
+ twin_ax2 = plots [0 ][2 ].twinx ()
507
+
508
+ # Plot data to primary axes
509
+ df ["a" ].plot (ax = plots [0 ][0 ], title = "External share only" ).set_xlabel (
510
+ "this label should never be visible"
511
+ )
512
+ df ["a" ].plot (ax = plots [1 ][0 ])
513
+
514
+ df ["a" ].plot (ax = plots [0 ][1 ], title = "Internal share (twin) only" ).set_xlabel (
515
+ "this label should always be visible"
516
+ )
517
+ df ["a" ].plot (ax = plots [1 ][1 ])
518
+
519
+ df ["a" ].plot (ax = plots [0 ][2 ], title = "Both" ).set_xlabel (
520
+ "this label should never be visible"
521
+ )
522
+ df ["a" ].plot (ax = plots [1 ][2 ])
523
+
524
+ # Plot data to twinned axes
525
+ df ["b" ].plot (ax = twin_ax1 , color = "green" )
526
+ df ["b" ].plot (ax = twin_ax2 , color = "yellow" )
527
+
528
+ assert not plots [0 ][0 ].xaxis .get_label ().get_visible ()
529
+ assert plots [0 ][1 ].xaxis .get_label ().get_visible ()
530
+ assert not plots [0 ][2 ].xaxis .get_label ().get_visible ()
0 commit comments