@@ -188,6 +188,18 @@ def test_gca():
188
188
assert fig .gca (polar = True ) is not ax2
189
189
assert fig .gca ().get_subplotspec ().get_geometry () == (1 , 2 , 1 , 1 )
190
190
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
+
191
203
fig .sca (ax1 )
192
204
with pytest .warns (
193
205
MatplotlibDeprecationWarning ,
@@ -244,6 +256,11 @@ def test_add_subplot_invalid():
244
256
match = 'Passing non-integers as three-element position '
245
257
'specification is deprecated' ):
246
258
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 )
247
264
248
265
249
266
@image_comparison (['figure_suptitle' ])
@@ -429,6 +446,12 @@ def test_invalid_figure_add_axes():
429
446
with pytest .raises (TypeError , match = "multiple values for argument 'rect'" ):
430
447
fig .add_axes ([0 , 0 , 1 , 1 ], rect = [0 , 0 , 1 , 1 ])
431
448
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
+
432
455
433
456
def test_subplots_shareax_loglabels ():
434
457
fig , axs = plt .subplots (2 , 2 , sharex = True , sharey = True , squeeze = False )
0 commit comments