@@ -2425,8 +2425,7 @@ def test_pyplot_axes():
24252425
24262426@image_comparison (['log_scales' ])
24272427def test_log_scales ():
2428- fig = plt .figure ()
2429- ax = fig .add_subplot (1 , 1 , 1 )
2428+ fig , ax = plt .subplots ()
24302429 ax .plot (np .log (np .linspace (0.1 , 100 )))
24312430 ax .set_yscale ('log' , base = 5.5 )
24322431 ax .invert_yaxis ()
@@ -2441,8 +2440,7 @@ def test_log_scales_no_data():
24412440
24422441
24432442def test_log_scales_invalid ():
2444- fig = plt .figure ()
2445- ax = fig .add_subplot (1 , 1 , 1 )
2443+ fig , ax = plt .subplots ()
24462444 ax .set_xscale ('log' )
24472445 with pytest .warns (UserWarning , match = 'Attempted to set non-positive' ):
24482446 ax .set_xlim (- 1 , 10 )
@@ -2465,8 +2463,7 @@ def test_stackplot():
24652463
24662464 # Reuse testcase from above for a labeled data test
24672465 data = {"x" : x , "y1" : y1 , "y2" : y2 , "y3" : y3 }
2468- fig = plt .figure ()
2469- ax = fig .add_subplot (1 , 1 , 1 )
2466+ fig , ax = plt .subplots ()
24702467 ax .stackplot ("x" , "y1" , "y2" , "y3" , data = data )
24712468 ax .set_xlim ((0 , 10 ))
24722469 ax .set_ylim ((0 , 70 ))
@@ -3317,27 +3314,26 @@ def test_errorbar_limits():
33173314 yerr = 0.2
33183315 ls = 'dotted'
33193316
3320- fig = plt .figure ()
3321- ax = fig .add_subplot (1 , 1 , 1 )
3317+ fig , ax = plt .subplots ()
33223318
33233319 # standard error bars
3324- plt .errorbar (x , y , xerr = xerr , yerr = yerr , ls = ls , color = 'blue' )
3320+ ax .errorbar (x , y , xerr = xerr , yerr = yerr , ls = ls , color = 'blue' )
33253321
33263322 # including upper limits
33273323 uplims = np .zeros_like (x )
33283324 uplims [[1 , 5 , 9 ]] = True
3329- plt .errorbar (x , y + 0.5 , xerr = xerr , yerr = yerr , uplims = uplims , ls = ls ,
3330- color = 'green' )
3325+ ax .errorbar (x , y + 0.5 , xerr = xerr , yerr = yerr , uplims = uplims , ls = ls ,
3326+ color = 'green' )
33313327
33323328 # including lower limits
33333329 lolims = np .zeros_like (x )
33343330 lolims [[2 , 4 , 8 ]] = True
3335- plt .errorbar (x , y + 1.0 , xerr = xerr , yerr = yerr , lolims = lolims , ls = ls ,
3336- color = 'red' )
3331+ ax .errorbar (x , y + 1.0 , xerr = xerr , yerr = yerr , lolims = lolims , ls = ls ,
3332+ color = 'red' )
33373333
33383334 # including upper and lower limits
3339- plt .errorbar (x , y + 1.5 , marker = 'o' , ms = 8 , xerr = xerr , yerr = yerr ,
3340- lolims = lolims , uplims = uplims , ls = ls , color = 'magenta' )
3335+ ax .errorbar (x , y + 1.5 , marker = 'o' , ms = 8 , xerr = xerr , yerr = yerr ,
3336+ lolims = lolims , uplims = uplims , ls = ls , color = 'magenta' )
33413337
33423338 # including xlower and xupper limits
33433339 xerr = 0.2
@@ -3349,10 +3345,10 @@ def test_errorbar_limits():
33493345 uplims = np .zeros_like (x )
33503346 lolims [[6 ]] = True
33513347 uplims [[3 ]] = True
3352- plt .errorbar (x , y + 2.1 , marker = 'o' , ms = 8 , xerr = xerr , yerr = yerr ,
3353- xlolims = xlolims , xuplims = xuplims , uplims = uplims ,
3354- lolims = lolims , ls = 'none' , mec = 'blue' , capsize = 0 ,
3355- color = 'cyan' )
3348+ ax .errorbar (x , y + 2.1 , marker = 'o' , ms = 8 , xerr = xerr , yerr = yerr ,
3349+ xlolims = xlolims , xuplims = xuplims , uplims = uplims ,
3350+ lolims = lolims , ls = 'none' , mec = 'blue' , capsize = 0 ,
3351+ color = 'cyan' )
33563352 ax .set_xlim ((0 , 5.5 ))
33573353 ax .set_title ('Errorbar upper and lower limits' )
33583354
@@ -3484,13 +3480,11 @@ def test_stem(use_line_collection):
34843480 ax .stem (x , np .cos (x ),
34853481 linefmt = 'C2-.' , markerfmt = 'k+' , basefmt = 'C1-.' , label = ' ' ,
34863482 use_line_collection = use_line_collection )
3487-
34883483 ax .legend ()
34893484
34903485
34913486def test_stem_args ():
3492- fig = plt .figure ()
3493- ax = fig .add_subplot (1 , 1 , 1 )
3487+ fig , ax = plt .subplots ()
34943488
34953489 x = list (range (10 ))
34963490 y = list (range (10 ))
@@ -4229,8 +4223,7 @@ def test_step_linestyle():
42294223@image_comparison (['mixed_collection' ], remove_text = True )
42304224def test_mixed_collection ():
42314225 # First illustrate basic pyplot interface, using defaults where possible.
4232- fig = plt .figure ()
4233- ax = fig .add_subplot (1 , 1 , 1 )
4226+ fig , ax = plt .subplots ()
42344227
42354228 c = mpatches .Circle ((8 , 8 ), radius = 4 , facecolor = 'none' , edgecolor = 'green' )
42364229
@@ -4571,8 +4564,7 @@ def test_rcparam_grid_minor():
45714564
45724565 for locator , result in values :
45734566 matplotlib .rcParams ['axes.grid.which' ] = locator
4574- fig = plt .figure ()
4575- ax = fig .add_subplot (1 , 1 , 1 )
4567+ fig , ax = plt .subplots ()
45764568 assert (ax .xaxis ._gridOnMajor , ax .xaxis ._gridOnMinor ) == result
45774569
45784570 matplotlib .rcParams ['axes.grid' ] = orig_grid
@@ -5786,16 +5778,14 @@ def test_title_no_move_off_page():
57865778
57875779def test_offset_label_color ():
57885780 # Tests issue 6440
5789- fig = plt .figure ()
5790- ax = fig .add_subplot (1 , 1 , 1 )
5781+ fig , ax = plt .subplots ()
57915782 ax .plot ([1.01e9 , 1.02e9 , 1.03e9 ])
57925783 ax .yaxis .set_tick_params (labelcolor = 'red' )
57935784 assert ax .yaxis .get_offset_text ().get_color () == 'red'
57945785
57955786
57965787def test_offset_text_visible ():
5797- fig = plt .figure ()
5798- ax = fig .add_subplot (1 , 1 , 1 )
5788+ fig , ax = plt .subplots ()
57995789 ax .plot ([1.01e9 , 1.02e9 , 1.03e9 ])
58005790 ax .yaxis .set_tick_params (label1On = False , label2On = True )
58015791 assert ax .yaxis .get_offset_text ().get_visible ()
0 commit comments