@@ -188,6 +188,18 @@ def test_gca():
188188 assert fig .gca (polar = True ) is not ax2
189189 assert fig .gca ().get_subplotspec ().get_geometry () == (1 , 2 , 1 , 1 )
190190
191+ # add_axes on an existing Axes should not change stored order, but will
192+ # make it current.
193+ fig .add_axes (ax0 )
194+ assert fig .axes == [ax0 , ax1 , ax2 , ax3 ]
195+ assert fig .gca () is ax0
196+
197+ # add_subplot on an existing Axes should not change stored order, but will
198+ # make it current.
199+ fig .add_subplot (ax2 )
200+ assert fig .axes == [ax0 , ax1 , ax2 , ax3 ]
201+ assert fig .gca () is ax2
202+
191203 fig .sca (ax1 )
192204 with pytest .warns (
193205 MatplotlibDeprecationWarning ,
@@ -244,6 +256,11 @@ def test_add_subplot_invalid():
244256 match = 'Passing non-integers as three-element position '
245257 'specification is deprecated' ):
246258 fig .add_subplot (2.0 , 2 , 1 )
259+ _ , ax = plt .subplots ()
260+ with pytest .raises (ValueError ,
261+ match = 'The Subplot must have been created in the '
262+ 'present figure' ):
263+ fig .add_subplot (ax )
247264
248265
249266@image_comparison (['figure_suptitle' ])
@@ -429,6 +446,12 @@ def test_invalid_figure_add_axes():
429446 with pytest .raises (TypeError , match = "multiple values for argument 'rect'" ):
430447 fig .add_axes ([0 , 0 , 1 , 1 ], rect = [0 , 0 , 1 , 1 ])
431448
449+ _ , ax = plt .subplots ()
450+ with pytest .raises (ValueError ,
451+ match = "The Axes must have been created in the present "
452+ "figure" ):
453+ fig .add_axes (ax )
454+
432455
433456def test_subplots_shareax_loglabels ():
434457 fig , axs = plt .subplots (2 , 2 , sharex = True , sharey = True , squeeze = False )
0 commit comments