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