27
27
)
28
28
29
29
mpl = pytest .importorskip ("matplotlib" )
30
+ plt = pytest .importorskip ("matplotlib.pyplot" )
31
+
32
+ from pandas .plotting ._matplotlib .hist import _grouped_hist
30
33
31
34
32
35
@pytest .fixture
@@ -119,18 +122,13 @@ def test_hist_layout_with_by_shape(self, hist_df):
119
122
_check_axes_shape (axes , axes_num = 4 , layout = (4 , 2 ), figsize = (12 , 7 ))
120
123
121
124
def test_hist_no_overlap (self ):
122
- from matplotlib .pyplot import (
123
- gcf ,
124
- subplot ,
125
- )
126
-
127
125
x = Series (np .random .default_rng (2 ).standard_normal (2 ))
128
126
y = Series (np .random .default_rng (2 ).standard_normal (2 ))
129
- subplot (121 )
127
+ plt . subplot (121 )
130
128
x .hist ()
131
- subplot (122 )
129
+ plt . subplot (122 )
132
130
y .hist ()
133
- fig = gcf ()
131
+ fig = plt . gcf ()
134
132
axes = fig .axes
135
133
assert len (axes ) == 2
136
134
@@ -140,10 +138,8 @@ def test_hist_by_no_extra_plots(self, hist_df):
140
138
assert len (mpl .pyplot .get_fignums ()) == 1
141
139
142
140
def test_plot_fails_when_ax_differs_from_figure (self , ts ):
143
- from pylab import figure
144
-
145
- fig1 = figure ()
146
- fig2 = figure ()
141
+ fig1 = plt .figure (1 )
142
+ fig2 = plt .figure (2 )
147
143
ax1 = fig1 .add_subplot (111 )
148
144
msg = "passed axis not bound to passed figure"
149
145
with pytest .raises (AssertionError , match = msg ):
@@ -169,8 +165,8 @@ def test_histtype_argument(self, histtype, expected):
169
165
)
170
166
def test_hist_with_legend (self , by , expected_axes_num , expected_layout ):
171
167
# GH 6279 - Series histogram can have a legend
172
- index = 15 * ["1" ] + 15 * ["2" ]
173
- s = Series (np .random .default_rng (2 ).standard_normal (30 ), index = index , name = "a" )
168
+ index = 5 * ["1" ] + 5 * ["2" ]
169
+ s = Series (np .random .default_rng (2 ).standard_normal (10 ), index = index , name = "a" )
174
170
s .index .name = "b"
175
171
176
172
# Use default_axes=True when plotting method generate subplots itself
@@ -181,8 +177,8 @@ def test_hist_with_legend(self, by, expected_axes_num, expected_layout):
181
177
@pytest .mark .parametrize ("by" , [None , "b" ])
182
178
def test_hist_with_legend_raises (self , by ):
183
179
# GH 6279 - Series histogram with legend and label raises
184
- index = 15 * ["1" ] + 15 * ["2" ]
185
- s = Series (np .random .default_rng (2 ).standard_normal (30 ), index = index , name = "a" )
180
+ index = 5 * ["1" ] + 5 * ["2" ]
181
+ s = Series (np .random .default_rng (2 ).standard_normal (10 ), index = index , name = "a" )
186
182
s .index .name = "b"
187
183
188
184
with pytest .raises (ValueError , match = "Cannot use both legend and label" ):
@@ -331,12 +327,10 @@ def test_hist_df_legacy_layout_labelsize_rot(self, frame_or_series):
331
327
332
328
@pytest .mark .slow
333
329
def test_hist_df_legacy_rectangles (self ):
334
- from matplotlib .patches import Rectangle
335
-
336
330
ser = Series (range (10 ))
337
331
ax = ser .hist (cumulative = True , bins = 4 , density = True )
338
332
# height of last bin (index 5) must be 1.0
339
- rects = [x for x in ax .get_children () if isinstance (x , Rectangle )]
333
+ rects = [x for x in ax .get_children () if isinstance (x , mpl . patches . Rectangle )]
340
334
tm .assert_almost_equal (rects [- 1 ].get_height (), 1.0 )
341
335
342
336
@pytest .mark .slow
@@ -431,12 +425,12 @@ def test_hist_layout_error(self):
431
425
432
426
# GH 9351
433
427
def test_tight_layout (self ):
434
- df = DataFrame (np .random .default_rng (2 ).standard_normal ((100 , 2 )))
428
+ df = DataFrame (np .random .default_rng (2 ).standard_normal ((10 , 2 )))
435
429
df [2 ] = to_datetime (
436
430
np .random .default_rng (2 ).integers (
437
431
812419200000000000 ,
438
432
819331200000000000 ,
439
- size = 100 ,
433
+ size = 10 ,
440
434
dtype = np .int64 ,
441
435
)
442
436
)
@@ -504,7 +498,7 @@ def test_hist_column_order_unchanged(self, column, expected):
504
498
def test_histtype_argument (self , histtype , expected ):
505
499
# GH23992 Verify functioning of histtype argument
506
500
df = DataFrame (
507
- np .random .default_rng (2 ).integers (1 , 10 , size = (100 , 2 )), columns = ["a" , "b" ]
501
+ np .random .default_rng (2 ).integers (1 , 10 , size = (10 , 2 )), columns = ["a" , "b" ]
508
502
)
509
503
ax = df .hist (histtype = histtype )
510
504
_check_patches_all_filled (ax , filled = expected )
@@ -519,9 +513,9 @@ def test_hist_with_legend(self, by, column):
519
513
if by is not None :
520
514
expected_labels = [expected_labels ] * 2
521
515
522
- index = Index (15 * ["1" ] + 15 * ["2" ], name = "c" )
516
+ index = Index (5 * ["1" ] + 5 * ["2" ], name = "c" )
523
517
df = DataFrame (
524
- np .random .default_rng (2 ).standard_normal ((30 , 2 )),
518
+ np .random .default_rng (2 ).standard_normal ((10 , 2 )),
525
519
index = index ,
526
520
columns = ["a" , "b" ],
527
521
)
@@ -545,9 +539,9 @@ def test_hist_with_legend(self, by, column):
545
539
@pytest .mark .parametrize ("column" , [None , "b" ])
546
540
def test_hist_with_legend_raises (self , by , column ):
547
541
# GH 6279 - DataFrame histogram with legend and label raises
548
- index = Index (15 * ["1" ] + 15 * ["2" ], name = "c" )
542
+ index = Index (5 * ["1" ] + 5 * ["2" ], name = "c" )
549
543
df = DataFrame (
550
- np .random .default_rng (2 ).standard_normal ((30 , 2 )),
544
+ np .random .default_rng (2 ).standard_normal ((10 , 2 )),
551
545
index = index ,
552
546
columns = ["a" , "b" ],
553
547
)
@@ -586,7 +580,7 @@ def test_hist_df_with_nonnumerics_no_bins(self):
586
580
def test_hist_secondary_legend (self ):
587
581
# GH 9610
588
582
df = DataFrame (
589
- np .random .default_rng (2 ).standard_normal ((30 , 4 )), columns = list ("abcd" )
583
+ np .random .default_rng (2 ).standard_normal ((10 , 4 )), columns = list ("abcd" )
590
584
)
591
585
592
586
# primary -> secondary
@@ -602,7 +596,7 @@ def test_hist_secondary_legend(self):
602
596
def test_hist_secondary_secondary (self ):
603
597
# GH 9610
604
598
df = DataFrame (
605
- np .random .default_rng (2 ).standard_normal ((30 , 4 )), columns = list ("abcd" )
599
+ np .random .default_rng (2 ).standard_normal ((10 , 4 )), columns = list ("abcd" )
606
600
)
607
601
# secondary -> secondary
608
602
_ , ax = mpl .pyplot .subplots ()
@@ -617,7 +611,7 @@ def test_hist_secondary_secondary(self):
617
611
def test_hist_secondary_primary (self ):
618
612
# GH 9610
619
613
df = DataFrame (
620
- np .random .default_rng (2 ).standard_normal ((30 , 4 )), columns = list ("abcd" )
614
+ np .random .default_rng (2 ).standard_normal ((10 , 4 )), columns = list ("abcd" )
621
615
)
622
616
# secondary -> primary
623
617
_ , ax = mpl .pyplot .subplots ()
@@ -632,7 +626,6 @@ def test_hist_secondary_primary(self):
632
626
633
627
def test_hist_with_nans_and_weights (self ):
634
628
# GH 48884
635
- mpl_patches = pytest .importorskip ("matplotlib.patches" )
636
629
df = DataFrame (
637
630
[[np .nan , 0.2 , 0.3 ], [0.4 , np .nan , np .nan ], [0.7 , 0.8 , 0.9 ]],
638
631
columns = list ("abc" ),
@@ -643,12 +636,12 @@ def test_hist_with_nans_and_weights(self):
643
636
644
637
_ , ax0 = mpl .pyplot .subplots ()
645
638
df .plot .hist (ax = ax0 , weights = weights )
646
- rects = [x for x in ax0 .get_children () if isinstance (x , mpl_patches .Rectangle )]
639
+ rects = [x for x in ax0 .get_children () if isinstance (x , mpl . patches .Rectangle )]
647
640
heights = [rect .get_height () for rect in rects ]
648
641
_ , ax1 = mpl .pyplot .subplots ()
649
642
no_nan_df .plot .hist (ax = ax1 , weights = no_nan_weights )
650
643
no_nan_rects = [
651
- x for x in ax1 .get_children () if isinstance (x , mpl_patches .Rectangle )
644
+ x for x in ax1 .get_children () if isinstance (x , mpl . patches .Rectangle )
652
645
]
653
646
no_nan_heights = [rect .get_height () for rect in no_nan_rects ]
654
647
assert all (h0 == h1 for h0 , h1 in zip (heights , no_nan_heights ))
@@ -663,8 +656,6 @@ def test_hist_with_nans_and_weights(self):
663
656
664
657
class TestDataFrameGroupByPlots :
665
658
def test_grouped_hist_legacy (self ):
666
- from pandas .plotting ._matplotlib .hist import _grouped_hist
667
-
668
659
rs = np .random .default_rng (10 )
669
660
df = DataFrame (rs .standard_normal ((10 , 1 )), columns = ["A" ])
670
661
df ["B" ] = to_datetime (
@@ -716,10 +707,6 @@ def test_grouped_hist_legacy_single_key(self):
716
707
_check_ticks_props (axes , xrot = 30 )
717
708
718
709
def test_grouped_hist_legacy_grouped_hist_kwargs (self ):
719
- from matplotlib .patches import Rectangle
720
-
721
- from pandas .plotting ._matplotlib .hist import _grouped_hist
722
-
723
710
rs = np .random .default_rng (2 )
724
711
df = DataFrame (rs .standard_normal ((10 , 1 )), columns = ["A" ])
725
712
df ["B" ] = to_datetime (
@@ -748,14 +735,14 @@ def test_grouped_hist_legacy_grouped_hist_kwargs(self):
748
735
)
749
736
# height of last bin (index 5) must be 1.0
750
737
for ax in axes .ravel ():
751
- rects = [x for x in ax .get_children () if isinstance (x , Rectangle )]
738
+ rects = [
739
+ x for x in ax .get_children () if isinstance (x , mpl .patches .Rectangle )
740
+ ]
752
741
height = rects [- 1 ].get_height ()
753
742
tm .assert_almost_equal (height , 1.0 )
754
743
_check_ticks_props (axes , xlabelsize = xf , xrot = xrot , ylabelsize = yf , yrot = yrot )
755
744
756
745
def test_grouped_hist_legacy_grouped_hist (self ):
757
- from pandas .plotting ._matplotlib .hist import _grouped_hist
758
-
759
746
rs = np .random .default_rng (2 )
760
747
df = DataFrame (rs .standard_normal ((10 , 1 )), columns = ["A" ])
761
748
df ["B" ] = to_datetime (
@@ -773,8 +760,6 @@ def test_grouped_hist_legacy_grouped_hist(self):
773
760
_check_ax_scales (axes , yaxis = "log" )
774
761
775
762
def test_grouped_hist_legacy_external_err (self ):
776
- from pandas .plotting ._matplotlib .hist import _grouped_hist
777
-
778
763
rs = np .random .default_rng (2 )
779
764
df = DataFrame (rs .standard_normal ((10 , 1 )), columns = ["A" ])
780
765
df ["B" ] = to_datetime (
0 commit comments