@@ -670,7 +670,7 @@ def test_hist_layout_with_by(self):
670
670
axes = _check_plot_works (df .height .hist , by = df .classroom , layout = (2 , 2 ))
671
671
self ._check_axes_shape (axes , axes_num = 3 , layout = (2 , 2 ))
672
672
673
- axes = _check_plot_works ( df .height .hist , by = df .category , layout = (4 , 2 ), figsize = (12 , 7 ))
673
+ axes = df .height .hist ( by = df .category , layout = (4 , 2 ), figsize = (12 , 7 ))
674
674
self ._check_axes_shape (axes , axes_num = 4 , layout = (4 , 2 ), figsize = (12 , 7 ))
675
675
676
676
@slow
@@ -1071,6 +1071,7 @@ def test_subplots(self):
1071
1071
for kind in ['bar' , 'barh' , 'line' , 'area' ]:
1072
1072
axes = df .plot (kind = kind , subplots = True , sharex = True , legend = True )
1073
1073
self ._check_axes_shape (axes , axes_num = 3 , layout = (3 , 1 ))
1074
+ self .assertEqual (axes .shape , (3 , ))
1074
1075
1075
1076
for ax , column in zip (axes , df .columns ):
1076
1077
self ._check_legend_labels (ax , labels = [com .pprint_thing (column )])
@@ -1133,6 +1134,77 @@ def test_subplots_timeseries(self):
1133
1134
self ._check_visible (ax .get_yticklabels ())
1134
1135
self ._check_ticks_props (ax , xlabelsize = 7 , xrot = 45 )
1135
1136
1137
+ def test_subplots_layout (self ):
1138
+ # GH 6667
1139
+ df = DataFrame (np .random .rand (10 , 3 ),
1140
+ index = list (string .ascii_letters [:10 ]))
1141
+
1142
+ axes = df .plot (subplots = True , layout = (2 , 2 ))
1143
+ self ._check_axes_shape (axes , axes_num = 3 , layout = (2 , 2 ))
1144
+ self .assertEqual (axes .shape , (2 , 2 ))
1145
+
1146
+ axes = df .plot (subplots = True , layout = (1 , 4 ))
1147
+ self ._check_axes_shape (axes , axes_num = 3 , layout = (1 , 4 ))
1148
+ self .assertEqual (axes .shape , (1 , 4 ))
1149
+
1150
+ with tm .assertRaises (ValueError ):
1151
+ axes = df .plot (subplots = True , layout = (1 , 1 ))
1152
+
1153
+ # single column
1154
+ df = DataFrame (np .random .rand (10 , 1 ),
1155
+ index = list (string .ascii_letters [:10 ]))
1156
+ axes = df .plot (subplots = True )
1157
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
1158
+ self .assertEqual (axes .shape , (1 , ))
1159
+
1160
+ axes = df .plot (subplots = True , layout = (3 , 3 ))
1161
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (3 , 3 ))
1162
+ self .assertEqual (axes .shape , (3 , 3 ))
1163
+
1164
+ @slow
1165
+ def test_subplots_multiple_axes (self ):
1166
+ # GH 5353, 6970, GH 7069
1167
+ fig , axes = self .plt .subplots (2 , 3 )
1168
+ df = DataFrame (np .random .rand (10 , 3 ),
1169
+ index = list (string .ascii_letters [:10 ]))
1170
+
1171
+ returned = df .plot (subplots = True , ax = axes [0 ])
1172
+ self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
1173
+ self .assertEqual (returned .shape , (3 , ))
1174
+ self .assertIs (returned [0 ].figure , fig )
1175
+ # draw on second row
1176
+ returned = df .plot (subplots = True , ax = axes [1 ])
1177
+ self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
1178
+ self .assertEqual (returned .shape , (3 , ))
1179
+ self .assertIs (returned [0 ].figure , fig )
1180
+ self ._check_axes_shape (axes , axes_num = 6 , layout = (2 , 3 ))
1181
+ tm .close ()
1182
+
1183
+ with tm .assertRaises (ValueError ):
1184
+ fig , axes = self .plt .subplots (2 , 3 )
1185
+ # pass different number of axes from required
1186
+ df .plot (subplots = True , ax = axes )
1187
+
1188
+ # pass 2-dim axes and invalid layout
1189
+ # invalid lauout should not affect to input and return value
1190
+ # (show warning is tested in
1191
+ # TestDataFrameGroupByPlots.test_grouped_box_multiple_axes
1192
+ fig , axes = self .plt .subplots (2 , 2 )
1193
+ df = DataFrame (np .random .rand (10 , 4 ),
1194
+ index = list (string .ascii_letters [:10 ]))
1195
+
1196
+ returned = df .plot (subplots = True , ax = axes , layout = (2 , 1 ))
1197
+ self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
1198
+ self .assertEqual (returned .shape , (4 , ))
1199
+
1200
+ # single column
1201
+ fig , axes = self .plt .subplots (1 , 1 )
1202
+ df = DataFrame (np .random .rand (10 , 1 ),
1203
+ index = list (string .ascii_letters [:10 ]))
1204
+ axes = df .plot (subplots = True , ax = [axes ])
1205
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
1206
+ self .assertEqual (axes .shape , (1 , ))
1207
+
1136
1208
def test_negative_log (self ):
1137
1209
df = - DataFrame (rand (6 , 4 ),
1138
1210
index = list (string .ascii_letters [:6 ]),
@@ -2733,6 +2805,41 @@ def test_grouped_box_layout(self):
2733
2805
return_type = 'dict' )
2734
2806
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 , layout = (1 , 4 ))
2735
2807
2808
+ @slow
2809
+ def test_grouped_box_multiple_axes (self ):
2810
+ # GH 6970, GH 7069
2811
+ df = self .hist_df
2812
+
2813
+ # check warning to ignore sharex / sharey
2814
+ # this check should be done in the first function which
2815
+ # passes multiple axes to plot, hist or boxplot
2816
+ # location should be changed if other test is added
2817
+ # which has earlier alphabetical order
2818
+ with tm .assert_produces_warning (UserWarning ):
2819
+ fig , axes = self .plt .subplots (2 , 2 )
2820
+ df .groupby ('category' ).boxplot (column = 'height' , return_type = 'axes' , ax = axes )
2821
+ self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 , layout = (2 , 2 ))
2822
+
2823
+ fig , axes = self .plt .subplots (2 , 3 )
2824
+ returned = df .boxplot (column = ['height' , 'weight' , 'category' ], by = 'gender' ,
2825
+ return_type = 'axes' , ax = axes [0 ])
2826
+ returned = np .array (returned .values ())
2827
+ self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
2828
+ self .assert_numpy_array_equal (returned , axes [0 ])
2829
+ self .assertIs (returned [0 ].figure , fig )
2830
+ # draw on second row
2831
+ returned = df .groupby ('classroom' ).boxplot (column = ['height' , 'weight' , 'category' ],
2832
+ return_type = 'axes' , ax = axes [1 ])
2833
+ returned = np .array (returned .values ())
2834
+ self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
2835
+ self .assert_numpy_array_equal (returned , axes [1 ])
2836
+ self .assertIs (returned [0 ].figure , fig )
2837
+
2838
+ with tm .assertRaises (ValueError ):
2839
+ fig , axes = self .plt .subplots (2 , 3 )
2840
+ # pass different number of axes from required
2841
+ axes = df .groupby ('classroom' ).boxplot (ax = axes )
2842
+
2736
2843
@slow
2737
2844
def test_grouped_hist_layout (self ):
2738
2845
@@ -2745,12 +2852,12 @@ def test_grouped_hist_layout(self):
2745
2852
axes = _check_plot_works (df .hist , column = 'height' , by = df .gender , layout = (2 , 1 ))
2746
2853
self ._check_axes_shape (axes , axes_num = 2 , layout = (2 , 1 ))
2747
2854
2748
- axes = _check_plot_works ( df .hist , column = 'height' , by = df .category , layout = (4 , 1 ))
2855
+ axes = df .hist ( column = 'height' , by = df .category , layout = (4 , 1 ))
2749
2856
self ._check_axes_shape (axes , axes_num = 4 , layout = (4 , 1 ))
2750
2857
2751
- axes = _check_plot_works (df .hist , column = 'height' , by = df .category ,
2752
- layout = (4 , 2 ), figsize = (12 , 8 ))
2858
+ axes = df .hist (column = 'height' , by = df .category , layout = (4 , 2 ), figsize = (12 , 8 ))
2753
2859
self ._check_axes_shape (axes , axes_num = 4 , layout = (4 , 2 ), figsize = (12 , 8 ))
2860
+ tm .close ()
2754
2861
2755
2862
# GH 6769
2756
2863
axes = _check_plot_works (df .hist , column = 'height' , by = 'classroom' , layout = (2 , 2 ))
@@ -2760,13 +2867,32 @@ def test_grouped_hist_layout(self):
2760
2867
axes = _check_plot_works (df .hist , by = 'classroom' )
2761
2868
self ._check_axes_shape (axes , axes_num = 3 , layout = (2 , 2 ))
2762
2869
2763
- axes = _check_plot_works ( df .hist , by = 'gender' , layout = (3 , 5 ))
2870
+ axes = df .hist ( by = 'gender' , layout = (3 , 5 ))
2764
2871
self ._check_axes_shape (axes , axes_num = 2 , layout = (3 , 5 ))
2765
2872
2766
- axes = _check_plot_works ( df .hist , column = ['height' , 'weight' , 'category' ])
2873
+ axes = df .hist ( column = ['height' , 'weight' , 'category' ])
2767
2874
self ._check_axes_shape (axes , axes_num = 3 , layout = (2 , 2 ))
2768
2875
2769
2876
@slow
2877
+ def test_grouped_hist_multiple_axes (self ):
2878
+ # GH 6970, GH 7069
2879
+ df = self .hist_df
2880
+
2881
+ fig , axes = self .plt .subplots (2 , 3 )
2882
+ returned = df .hist (column = ['height' , 'weight' , 'category' ], ax = axes [0 ])
2883
+ self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
2884
+ self .assert_numpy_array_equal (returned , axes [0 ])
2885
+ self .assertIs (returned [0 ].figure , fig )
2886
+ returned = df .hist (by = 'classroom' , ax = axes [1 ])
2887
+ self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
2888
+ self .assert_numpy_array_equal (returned , axes [1 ])
2889
+ self .assertIs (returned [0 ].figure , fig )
2890
+
2891
+ with tm .assertRaises (ValueError ):
2892
+ fig , axes = self .plt .subplots (2 , 3 )
2893
+ # pass different number of axes from required
2894
+ axes = df .hist (column = 'height' , ax = axes )
2895
+ @slow
2770
2896
def test_axis_share_x (self ):
2771
2897
df = self .hist_df
2772
2898
# GH4089
0 commit comments