16
16
from matplotlib ._api .deprecation import MatplotlibDeprecationWarning
17
17
from matplotlib .testing .decorators import image_comparison , check_figures_equal
18
18
from matplotlib .axes import Axes
19
- from matplotlib .figure import Figure
19
+ from matplotlib .figure import Figure , FigureBase
20
20
from matplotlib .layout_engine import (ConstrainedLayoutEngine ,
21
21
TightLayoutEngine )
22
22
from matplotlib .ticker import AutoMinorLocator , FixedFormatter , ScalarFormatter
@@ -709,7 +709,8 @@ def test_removed_axis():
709
709
fig .canvas .draw ()
710
710
711
711
712
- def test_figure_clear ():
712
+ @pytest .mark .parametrize ('clear_meth' , ['clear' , 'clf' ])
713
+ def test_figure_clear (clear_meth ):
713
714
# we test the following figure clearing scenarios:
714
715
fig = plt .figure ()
715
716
@@ -719,19 +720,19 @@ def test_figure_clear():
719
720
720
721
# b) a figure with a single unnested axes
721
722
ax = fig .add_subplot (111 )
722
- fig . clear ()
723
+ getattr ( fig , clear_meth ) ()
723
724
assert fig .axes == []
724
725
725
726
# c) a figure multiple unnested axes
726
727
axes = [fig .add_subplot (2 , 1 , i + 1 ) for i in range (2 )]
727
- fig . clear ()
728
+ getattr ( fig , clear_meth ) ()
728
729
assert fig .axes == []
729
730
730
731
# d) a figure with a subfigure
731
732
gs = fig .add_gridspec (ncols = 2 , nrows = 1 )
732
733
subfig = fig .add_subfigure (gs [0 ])
733
734
subaxes = subfig .add_subplot (111 )
734
- fig . clear ()
735
+ getattr ( fig , clear_meth ) ()
735
736
assert subfig not in fig .subfigs
736
737
assert fig .axes == []
737
738
@@ -755,15 +756,15 @@ def test_figure_clear():
755
756
subaxes = subfig .add_subplot (111 )
756
757
assert mainaxes in fig .axes
757
758
assert subaxes in fig .axes
758
- subfig . clear ()
759
+ getattr ( subfig , clear_meth ) ()
759
760
assert subfig in fig .subfigs
760
761
assert subaxes not in subfig .axes
761
762
assert subaxes not in fig .axes
762
763
assert mainaxes in fig .axes
763
764
764
765
# e.4) clearing the whole thing
765
766
subaxes = subfig .add_subplot (111 )
766
- fig . clear ()
767
+ getattr ( fig , clear_meth ) ()
767
768
assert fig .axes == []
768
769
assert fig .subfigs == []
769
770
@@ -774,22 +775,28 @@ def test_figure_clear():
774
775
assert all (sfig in fig .subfigs for sfig in subfigs )
775
776
776
777
# f.1) clearing only one subfigure
777
- subfigs [0 ]. clear ()
778
+ getattr ( subfigs [0 ], clear_meth ) ()
778
779
assert subaxes [0 ] not in fig .axes
779
780
assert subaxes [1 ] in fig .axes
780
781
assert subfigs [1 ] in fig .subfigs
781
782
782
783
# f.2) clearing the whole thing
783
- subfigs [1 ]. clear ()
784
+ getattr ( subfigs [1 ], clear_meth ) ()
784
785
subfigs = [fig .add_subfigure (gs [i ]) for i in [0 , 1 ]]
785
786
subaxes = [sfig .add_subplot (111 ) for sfig in subfigs ]
786
787
assert all (ax in fig .axes for ax in subaxes )
787
788
assert all (sfig in fig .subfigs for sfig in subfigs )
788
- fig . clear ()
789
+ getattr ( fig , clear_meth ) ()
789
790
assert fig .subfigs == []
790
791
assert fig .axes == []
791
792
792
793
794
+ def test_clf_not_refedined ():
795
+ for klass in FigureBase .__subclasses__ ():
796
+ # check that subclasses do not get redefined in our Figure subclasses
797
+ assert 'clf' not in klass .__dict__
798
+
799
+
793
800
@mpl .style .context ('mpl20' )
794
801
def test_picking_does_not_stale ():
795
802
fig , ax = plt .subplots ()
0 commit comments