@@ -755,6 +755,101 @@ def test_hist_no_overlap(self):
755
755
axes = fig .get_axes ()
756
756
self .assertEqual (len (axes ), 2 )
757
757
758
+ @slow
759
+ def test_hist_secondary_legend (self ):
760
+ # GH 9610
761
+ df = DataFrame (np .random .randn (30 , 4 ), columns = list ('abcd' ))
762
+
763
+ # primary -> secondary
764
+ ax = df ['a' ].plot (kind = 'hist' , legend = True )
765
+ df ['b' ].plot (kind = 'hist' , ax = ax , legend = True , secondary_y = True )
766
+ # both legends are dran on left ax
767
+ # left and right axis must be visible
768
+ self ._check_legend_labels (ax , labels = ['a' , 'b (right)' ])
769
+ self .assertTrue (ax .get_yaxis ().get_visible ())
770
+ self .assertTrue (ax .right_ax .get_yaxis ().get_visible ())
771
+ tm .close ()
772
+
773
+ # secondary -> secondary
774
+ ax = df ['a' ].plot (kind = 'hist' , legend = True , secondary_y = True )
775
+ df ['b' ].plot (kind = 'hist' , ax = ax , legend = True , secondary_y = True )
776
+ # both legends are draw on left ax
777
+ # left axis must be invisible, right axis must be visible
778
+ self ._check_legend_labels (ax .left_ax , labels = ['a (right)' , 'b (right)' ])
779
+ self .assertFalse (ax .left_ax .get_yaxis ().get_visible ())
780
+ self .assertTrue (ax .get_yaxis ().get_visible ())
781
+ tm .close ()
782
+
783
+ # secondary -> primary
784
+ ax = df ['a' ].plot (kind = 'hist' , legend = True , secondary_y = True )
785
+ # right axes is returned
786
+ df ['b' ].plot (kind = 'hist' , ax = ax , legend = True )
787
+ # both legends are draw on left ax
788
+ # left and right axis must be visible
789
+ self ._check_legend_labels (ax .left_ax , labels = ['a (right)' , 'b' ])
790
+ self .assertTrue (ax .left_ax .get_yaxis ().get_visible ())
791
+ self .assertTrue (ax .get_yaxis ().get_visible ())
792
+ tm .close ()
793
+
794
+ @slow
795
+ def test_df_series_secondary_legend (self ):
796
+ # GH 9779
797
+ df = DataFrame (np .random .randn (30 , 3 ), columns = list ('abc' ))
798
+ s = Series (np .random .randn (30 ), name = 'x' )
799
+
800
+ # primary -> secondary (without passing ax)
801
+ ax = df .plot ()
802
+ s .plot (legend = True , secondary_y = True )
803
+ # both legends are dran on left ax
804
+ # left and right axis must be visible
805
+ self ._check_legend_labels (ax , labels = ['a' , 'b' , 'c' , 'x (right)' ])
806
+ self .assertTrue (ax .get_yaxis ().get_visible ())
807
+ self .assertTrue (ax .right_ax .get_yaxis ().get_visible ())
808
+ tm .close ()
809
+
810
+ # primary -> secondary (with passing ax)
811
+ ax = df .plot ()
812
+ s .plot (ax = ax , legend = True , secondary_y = True )
813
+ # both legends are dran on left ax
814
+ # left and right axis must be visible
815
+ self ._check_legend_labels (ax , labels = ['a' , 'b' , 'c' , 'x (right)' ])
816
+ self .assertTrue (ax .get_yaxis ().get_visible ())
817
+ self .assertTrue (ax .right_ax .get_yaxis ().get_visible ())
818
+ tm .close ()
819
+
820
+ # seconcary -> secondary (without passing ax)
821
+ ax = df .plot (secondary_y = True )
822
+ s .plot (legend = True , secondary_y = True )
823
+ # both legends are dran on left ax
824
+ # left axis must be invisible and right axis must be visible
825
+ expected = ['a (right)' , 'b (right)' , 'c (right)' , 'x (right)' ]
826
+ self ._check_legend_labels (ax .left_ax , labels = expected )
827
+ self .assertFalse (ax .left_ax .get_yaxis ().get_visible ())
828
+ self .assertTrue (ax .get_yaxis ().get_visible ())
829
+ tm .close ()
830
+
831
+ # secondary -> secondary (with passing ax)
832
+ ax = df .plot (secondary_y = True )
833
+ s .plot (ax = ax , legend = True , secondary_y = True )
834
+ # both legends are dran on left ax
835
+ # left axis must be invisible and right axis must be visible
836
+ expected = ['a (right)' , 'b (right)' , 'c (right)' , 'x (right)' ]
837
+ self ._check_legend_labels (ax .left_ax , expected )
838
+ self .assertFalse (ax .left_ax .get_yaxis ().get_visible ())
839
+ self .assertTrue (ax .get_yaxis ().get_visible ())
840
+ tm .close ()
841
+
842
+ # secondary -> secondary (with passing ax)
843
+ ax = df .plot (secondary_y = True , mark_right = False )
844
+ s .plot (ax = ax , legend = True , secondary_y = True )
845
+ # both legends are dran on left ax
846
+ # left axis must be invisible and right axis must be visible
847
+ expected = ['a' , 'b' , 'c' , 'x (right)' ]
848
+ self ._check_legend_labels (ax .left_ax , expected )
849
+ self .assertFalse (ax .left_ax .get_yaxis ().get_visible ())
850
+ self .assertTrue (ax .get_yaxis ().get_visible ())
851
+ tm .close ()
852
+
758
853
@slow
759
854
def test_plot_fails_with_dupe_color_and_style (self ):
760
855
x = Series (randn (2 ))
0 commit comments