@@ -815,9 +815,7 @@ def test_axhspan_epoch():
815815 remove_text = True , extensions = ['png' ])
816816def test_hexbin_extent ():
817817 # this test exposes sf bug 2856228
818- fig = plt .figure ()
819-
820- ax = fig .add_subplot (111 )
818+ fig , ax = plt .subplots ()
821819 data = (np .arange (2000 ) / 2000 ).reshape ((2 , 1000 ))
822820 x , y = data
823821
@@ -826,8 +824,7 @@ def test_hexbin_extent():
826824 # Reuse testcase from above for a labeled data test
827825 data = {"x" : x , "y" : y }
828826
829- fig = plt .figure ()
830- ax = fig .add_subplot (111 )
827+ fig , ax = plt .subplots ()
831828 ax .hexbin ("x" , "y" , extent = [.1 , .3 , .6 , .7 ], data = data )
832829
833830
@@ -846,9 +843,7 @@ def __init__(self, x, y):
846843 self .x = x
847844 self .y = y
848845
849- fig = plt .figure ()
850-
851- ax = fig .add_subplot (111 )
846+ fig , ax = plt .subplots ()
852847 data = (np .arange (200 ) / 200 ).reshape ((2 , 100 ))
853848 x , y = data
854849 hb = ax .hexbin (x , y , extent = [.1 , .3 , .6 , .7 ], picker = - 1 )
@@ -861,32 +856,29 @@ def __init__(self, x, y):
861856 extensions = ['png' ])
862857def test_hexbin_log ():
863858 # Issue #1636
864- fig = plt .figure ()
865-
866859 np .random .seed (0 )
867860 n = 100000
868861 x = np .random .standard_normal (n )
869862 y = 2.0 + 3.0 * x + 4.0 * np .random .standard_normal (n )
870863 y = np .power (2 , y * 0.5 )
871- ax = fig .add_subplot (111 )
864+
865+ fig , ax = plt .subplots ()
872866 ax .hexbin (x , y , yscale = 'log' )
873867
874868
875869def test_inverted_limits ():
876870 # Test gh:1553
877871 # Calling invert_xaxis prior to plotting should not disable autoscaling
878872 # while still maintaining the inverted direction
879- fig = plt .figure ()
880- ax = fig .gca ()
873+ fig , ax = plt .subplots ()
881874 ax .invert_xaxis ()
882875 ax .plot ([- 5 , - 3 , 2 , 4 ], [1 , 2 , - 3 , 5 ])
883876
884877 assert ax .get_xlim () == (4 , - 5 )
885878 assert ax .get_ylim () == (- 3 , 5 )
886879 plt .close ()
887880
888- fig = plt .figure ()
889- ax = fig .gca ()
881+ fig , ax = plt .subplots ()
890882 ax .invert_yaxis ()
891883 ax .plot ([- 5 , - 3 , 2 , 4 ], [1 , 2 , - 3 , 5 ])
892884
@@ -905,8 +897,7 @@ def test_nonfinite_limits():
905897 finally :
906898 np .seterr (** olderr )
907899 x [len (x )// 2 ] = np .nan
908- fig = plt .figure ()
909- ax = fig .add_subplot (111 )
900+ fig , ax = plt .subplots ()
910901 ax .plot (x , y )
911902
912903
@@ -921,9 +912,7 @@ def test_imshow():
921912 r = np .sqrt (x ** 2 + y ** 2 - x * y )
922913
923914 # Create a contour plot at N/4 and extract both the clip path and transform
924- fig = plt .figure ()
925- ax = fig .add_subplot (111 )
926-
915+ fig , ax = plt .subplots ()
927916 ax .imshow (r )
928917
929918 # Reuse testcase from above for a labeled data test
@@ -945,8 +934,7 @@ def test_imshow_clip():
945934 r = np .sqrt (x ** 2 + y ** 2 - x * y )
946935
947936 # Create a contour plot at N/4 and extract both the clip path and transform
948- fig = plt .figure ()
949- ax = fig .add_subplot (111 )
937+ fig , ax = plt .subplots ()
950938
951939 c = ax .contour (r , [N / 4 ])
952940 x = c .collections [0 ]
@@ -967,8 +955,7 @@ def test_polycollection_joinstyle():
967955
968956 from matplotlib import collections as mcoll
969957
970- fig = plt .figure ()
971- ax = fig .add_subplot (111 )
958+ fig , ax = plt .subplots ()
972959 verts = np .array ([[1 , 1 ], [1 , 2 ], [2 , 2 ], [2 , 1 ]])
973960 c = mcoll .PolyCollection ([verts ], linewidths = 40 )
974961 ax .add_collection (c )
@@ -988,8 +975,7 @@ def test_polycollection_joinstyle():
988975 ]
989976)
990977def test_fill_between_input (x , y1 , y2 ):
991- fig = plt .figure ()
992- ax = fig .add_subplot (211 )
978+ fig , ax = plt .subplots ()
993979 with pytest .raises (ValueError ):
994980 ax .fill_between (x , y1 , y2 )
995981
@@ -1006,8 +992,7 @@ def test_fill_between_input(x, y1, y2):
1006992 ]
1007993)
1008994def test_fill_betweenx_input (y , x1 , x2 ):
1009- fig = plt .figure ()
1010- ax = fig .add_subplot (211 )
995+ fig , ax = plt .subplots ()
1011996 with pytest .raises (ValueError ):
1012997 ax .fill_betweenx (y , x1 , x2 )
1013998
@@ -1019,23 +1004,21 @@ def test_fill_between_interpolate():
10191004 y1 = np .sin (2 * np .pi * x )
10201005 y2 = 1.2 * np .sin (4 * np .pi * x )
10211006
1022- fig = plt .figure ()
1023- ax = fig .add_subplot (211 )
1024- ax .plot (x , y1 , x , y2 , color = 'black' )
1025- ax .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'white' , hatch = '/' ,
1026- interpolate = True )
1027- ax .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
1028- interpolate = True )
1007+ fig , (ax1 , ax2 ) = plt .subplots (2 , 1 , sharex = True )
1008+ ax1 .plot (x , y1 , x , y2 , color = 'black' )
1009+ ax1 .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'white' , hatch = '/' ,
1010+ interpolate = True )
1011+ ax1 .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
1012+ interpolate = True )
10291013
10301014 # Test support for masked arrays.
10311015 y2 = np .ma .masked_greater (y2 , 1.0 )
10321016 # Test that plotting works for masked arrays with the first element masked
10331017 y2 [0 ] = np .ma .masked
1034- ax1 = fig .add_subplot (212 , sharex = ax )
1035- ax1 .plot (x , y1 , x , y2 , color = 'black' )
1036- ax1 .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'green' ,
1018+ ax2 .plot (x , y1 , x , y2 , color = 'black' )
1019+ ax2 .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'green' ,
10371020 interpolate = True )
1038- ax1 .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
1021+ ax2 .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
10391022 interpolate = True )
10401023
10411024
@@ -1046,8 +1029,7 @@ def test_fill_between_interpolate_decreasing():
10461029 t = np .array ([9.4 , 7 , 2.2 ])
10471030 prof = np .array ([7.9 , 6.6 , 3.8 ])
10481031
1049- fig = plt .figure (figsize = (9 , 9 ))
1050- ax = fig .add_subplot (1 , 1 , 1 )
1032+ fig , ax = plt .subplots (figsize = (9 , 9 ))
10511033
10521034 ax .plot (t , p , 'tab:red' )
10531035 ax .plot (prof , p , 'k' )
@@ -1066,8 +1048,7 @@ def test_symlog():
10661048 x = np .array ([0 , 1 , 2 , 4 , 6 , 9 , 12 , 24 ])
10671049 y = np .array ([1000000 , 500000 , 100000 , 100 , 5 , 0 , 0 , 0 ])
10681050
1069- fig = plt .figure ()
1070- ax = fig .add_subplot (111 )
1051+ fig , ax = plt .subplots ()
10711052 ax .plot (x , y )
10721053 ax .set_yscale ('symlog' )
10731054 ax .set_xscale ('linear' )
@@ -1080,37 +1061,12 @@ def test_symlog2():
10801061 # Numbers from -50 to 50, with 0.1 as step
10811062 x = np .arange (- 50 , 50 , 0.001 )
10821063
1083- fig = plt .figure ()
1084- ax = fig .add_subplot (511 )
1085- # Plots a simple linear function 'f(x) = x'
1086- ax .plot (x , x )
1087- ax .set_xscale ('symlog' , linthreshx = 20.0 )
1088- ax .grid (True )
1089-
1090- ax = fig .add_subplot (512 )
1091- # Plots a simple linear function 'f(x) = x'
1092- ax .plot (x , x )
1093- ax .set_xscale ('symlog' , linthreshx = 2.0 )
1094- ax .grid (True )
1095-
1096- ax = fig .add_subplot (513 )
1097- # Plots a simple linear function 'f(x) = x'
1098- ax .plot (x , x )
1099- ax .set_xscale ('symlog' , linthreshx = 1.0 )
1100- ax .grid (True )
1101-
1102- ax = fig .add_subplot (514 )
1103- # Plots a simple linear function 'f(x) = x'
1104- ax .plot (x , x )
1105- ax .set_xscale ('symlog' , linthreshx = 0.1 )
1106- ax .grid (True )
1107-
1108- ax = fig .add_subplot (515 )
1109- # Plots a simple linear function 'f(x) = x'
1110- ax .plot (x , x )
1111- ax .set_xscale ('symlog' , linthreshx = 0.01 )
1112- ax .grid (True )
1113- ax .set_ylim (- 0.1 , 0.1 )
1064+ fig , axs = plt .subplots (5 , 1 )
1065+ for ax , linthreshx in zip (axs , [20. , 2. , 1. , 0.1 , 0.01 ]):
1066+ ax .plot (x , x )
1067+ ax .set_xscale ('symlog' , linthreshx = linthreshx )
1068+ ax .grid (True )
1069+ axs [- 1 ].set_ylim (- 0.1 , 0.1 )
11141070
11151071
11161072def test_pcolorargs_5205 ():
@@ -1142,15 +1098,10 @@ def test_pcolormesh():
11421098 # The color array can include masked values:
11431099 Zm = ma .masked_where (np .abs (Qz ) < 0.5 * np .max (Qz ), Z )
11441100
1145- fig = plt .figure ()
1146- ax = fig .add_subplot (131 )
1147- ax .pcolormesh (Qx , Qz , Z , lw = 0.5 , edgecolors = 'k' )
1148-
1149- ax = fig .add_subplot (132 )
1150- ax .pcolormesh (Qx , Qz , Z , lw = 2 , edgecolors = ['b' , 'w' ])
1151-
1152- ax = fig .add_subplot (133 )
1153- ax .pcolormesh (Qx , Qz , Z , shading = "gouraud" )
1101+ fig , (ax1 , ax2 , ax3 ) = plt .subplots (1 , 3 )
1102+ ax1 .pcolormesh (Qx , Qz , Z , lw = 0.5 , edgecolors = 'k' )
1103+ ax2 .pcolormesh (Qx , Qz , Z , lw = 2 , edgecolors = ['b' , 'w' ])
1104+ ax3 .pcolormesh (Qx , Qz , Z , shading = "gouraud" )
11541105
11551106
11561107@image_comparison (baseline_images = ['pcolormesh_datetime_axis' ],
0 commit comments