1616from matplotlib ._api .deprecation import MatplotlibDeprecationWarning
1717from matplotlib .testing .decorators import image_comparison , check_figures_equal
1818from matplotlib .axes import Axes
19- from matplotlib .figure import Figure
19+ from matplotlib .figure import Figure , FigureBase
2020from matplotlib .layout_engine import (ConstrainedLayoutEngine ,
2121 TightLayoutEngine )
2222from matplotlib .ticker import AutoMinorLocator , FixedFormatter , ScalarFormatter
@@ -709,7 +709,8 @@ def test_removed_axis():
709709 fig .canvas .draw ()
710710
711711
712- def test_figure_clear ():
712+ @pytest .mark .parametrize ('clear_meth' , ['clear' , 'clf' ])
713+ def test_figure_clear (clear_meth ):
713714 # we test the following figure clearing scenarios:
714715 fig = plt .figure ()
715716
@@ -719,19 +720,19 @@ def test_figure_clear():
719720
720721 # b) a figure with a single unnested axes
721722 ax = fig .add_subplot (111 )
722- fig . clear ()
723+ getattr ( fig , clear_meth ) ()
723724 assert fig .axes == []
724725
725726 # c) a figure multiple unnested axes
726727 axes = [fig .add_subplot (2 , 1 , i + 1 ) for i in range (2 )]
727- fig . clear ()
728+ getattr ( fig , clear_meth ) ()
728729 assert fig .axes == []
729730
730731 # d) a figure with a subfigure
731732 gs = fig .add_gridspec (ncols = 2 , nrows = 1 )
732733 subfig = fig .add_subfigure (gs [0 ])
733734 subaxes = subfig .add_subplot (111 )
734- fig . clear ()
735+ getattr ( fig , clear_meth ) ()
735736 assert subfig not in fig .subfigs
736737 assert fig .axes == []
737738
@@ -755,15 +756,15 @@ def test_figure_clear():
755756 subaxes = subfig .add_subplot (111 )
756757 assert mainaxes in fig .axes
757758 assert subaxes in fig .axes
758- subfig . clear ()
759+ getattr ( subfig , clear_meth ) ()
759760 assert subfig in fig .subfigs
760761 assert subaxes not in subfig .axes
761762 assert subaxes not in fig .axes
762763 assert mainaxes in fig .axes
763764
764765 # e.4) clearing the whole thing
765766 subaxes = subfig .add_subplot (111 )
766- fig . clear ()
767+ getattr ( fig , clear_meth ) ()
767768 assert fig .axes == []
768769 assert fig .subfigs == []
769770
@@ -774,22 +775,28 @@ def test_figure_clear():
774775 assert all (sfig in fig .subfigs for sfig in subfigs )
775776
776777 # f.1) clearing only one subfigure
777- subfigs [0 ]. clear ()
778+ getattr ( subfigs [0 ], clear_meth ) ()
778779 assert subaxes [0 ] not in fig .axes
779780 assert subaxes [1 ] in fig .axes
780781 assert subfigs [1 ] in fig .subfigs
781782
782783 # f.2) clearing the whole thing
783- subfigs [1 ]. clear ()
784+ getattr ( subfigs [1 ], clear_meth ) ()
784785 subfigs = [fig .add_subfigure (gs [i ]) for i in [0 , 1 ]]
785786 subaxes = [sfig .add_subplot (111 ) for sfig in subfigs ]
786787 assert all (ax in fig .axes for ax in subaxes )
787788 assert all (sfig in fig .subfigs for sfig in subfigs )
788- fig . clear ()
789+ getattr ( fig , clear_meth ) ()
789790 assert fig .subfigs == []
790791 assert fig .axes == []
791792
792793
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+
793800@mpl .style .context ('mpl20' )
794801def test_picking_does_not_stale ():
795802 fig , ax = plt .subplots ()
0 commit comments