@@ -821,9 +821,7 @@ def test_axhspan_epoch():
821821 remove_text = True , extensions = ['png' ])
822822def test_hexbin_extent ():
823823 # this test exposes sf bug 2856228
824- fig = plt .figure ()
825-
826- ax = fig .add_subplot (111 )
824+ fig , ax = plt .subplots ()
827825 data = (np .arange (2000 ) / 2000 ).reshape ((2 , 1000 ))
828826 x , y = data
829827
@@ -832,8 +830,7 @@ def test_hexbin_extent():
832830 # Reuse testcase from above for a labeled data test
833831 data = {"x" : x , "y" : y }
834832
835- fig = plt .figure ()
836- ax = fig .add_subplot (111 )
833+ fig , ax = plt .subplots ()
837834 ax .hexbin ("x" , "y" , extent = [.1 , .3 , .6 , .7 ], data = data )
838835
839836
@@ -852,9 +849,7 @@ def __init__(self, x, y):
852849 self .x = x
853850 self .y = y
854851
855- fig = plt .figure ()
856-
857- ax = fig .add_subplot (111 )
852+ fig , ax = plt .subplots ()
858853 data = (np .arange (200 ) / 200 ).reshape ((2 , 100 ))
859854 x , y = data
860855 hb = ax .hexbin (x , y , extent = [.1 , .3 , .6 , .7 ], picker = - 1 )
@@ -867,32 +862,29 @@ def __init__(self, x, y):
867862 extensions = ['png' ])
868863def test_hexbin_log ():
869864 # Issue #1636
870- fig = plt .figure ()
871-
872865 np .random .seed (0 )
873866 n = 100000
874867 x = np .random .standard_normal (n )
875868 y = 2.0 + 3.0 * x + 4.0 * np .random .standard_normal (n )
876869 y = np .power (2 , y * 0.5 )
877- ax = fig .add_subplot (111 )
870+
871+ fig , ax = plt .subplots ()
878872 ax .hexbin (x , y , yscale = 'log' )
879873
880874
881875def test_inverted_limits ():
882876 # Test gh:1553
883877 # Calling invert_xaxis prior to plotting should not disable autoscaling
884878 # while still maintaining the inverted direction
885- fig = plt .figure ()
886- ax = fig .gca ()
879+ fig , ax = plt .subplots ()
887880 ax .invert_xaxis ()
888881 ax .plot ([- 5 , - 3 , 2 , 4 ], [1 , 2 , - 3 , 5 ])
889882
890883 assert ax .get_xlim () == (4 , - 5 )
891884 assert ax .get_ylim () == (- 3 , 5 )
892885 plt .close ()
893886
894- fig = plt .figure ()
895- ax = fig .gca ()
887+ fig , ax = plt .subplots ()
896888 ax .invert_yaxis ()
897889 ax .plot ([- 5 , - 3 , 2 , 4 ], [1 , 2 , - 3 , 5 ])
898890
@@ -908,8 +900,7 @@ def test_nonfinite_limits():
908900 with np .errstate (divide = 'ignore' ):
909901 y = np .log (x )
910902 x [len (x )// 2 ] = np .nan
911- fig = plt .figure ()
912- ax = fig .add_subplot (111 )
903+ fig , ax = plt .subplots ()
913904 ax .plot (x , y )
914905
915906
@@ -924,9 +915,7 @@ def test_imshow():
924915 r = np .sqrt (x ** 2 + y ** 2 - x * y )
925916
926917 # Create a contour plot at N/4 and extract both the clip path and transform
927- fig = plt .figure ()
928- ax = fig .add_subplot (111 )
929-
918+ fig , ax = plt .subplots ()
930919 ax .imshow (r )
931920
932921 # Reuse testcase from above for a labeled data test
@@ -948,8 +937,7 @@ def test_imshow_clip():
948937 r = np .sqrt (x ** 2 + y ** 2 - x * y )
949938
950939 # Create a contour plot at N/4 and extract both the clip path and transform
951- fig = plt .figure ()
952- ax = fig .add_subplot (111 )
940+ fig , ax = plt .subplots ()
953941
954942 c = ax .contour (r , [N / 4 ])
955943 x = c .collections [0 ]
@@ -970,8 +958,7 @@ def test_polycollection_joinstyle():
970958
971959 from matplotlib import collections as mcoll
972960
973- fig = plt .figure ()
974- ax = fig .add_subplot (111 )
961+ fig , ax = plt .subplots ()
975962 verts = np .array ([[1 , 1 ], [1 , 2 ], [2 , 2 ], [2 , 1 ]])
976963 c = mcoll .PolyCollection ([verts ], linewidths = 40 )
977964 ax .add_collection (c )
@@ -991,8 +978,7 @@ def test_polycollection_joinstyle():
991978 ]
992979)
993980def test_fill_between_input (x , y1 , y2 ):
994- fig = plt .figure ()
995- ax = fig .add_subplot (211 )
981+ fig , ax = plt .subplots ()
996982 with pytest .raises (ValueError ):
997983 ax .fill_between (x , y1 , y2 )
998984
@@ -1009,8 +995,7 @@ def test_fill_between_input(x, y1, y2):
1009995 ]
1010996)
1011997def test_fill_betweenx_input (y , x1 , x2 ):
1012- fig = plt .figure ()
1013- ax = fig .add_subplot (211 )
998+ fig , ax = plt .subplots ()
1014999 with pytest .raises (ValueError ):
10151000 ax .fill_betweenx (y , x1 , x2 )
10161001
@@ -1022,23 +1007,21 @@ def test_fill_between_interpolate():
10221007 y1 = np .sin (2 * np .pi * x )
10231008 y2 = 1.2 * np .sin (4 * np .pi * x )
10241009
1025- fig = plt .figure ()
1026- ax = fig .add_subplot (211 )
1027- ax .plot (x , y1 , x , y2 , color = 'black' )
1028- ax .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'white' , hatch = '/' ,
1029- interpolate = True )
1030- ax .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
1031- interpolate = True )
1010+ fig , (ax1 , ax2 ) = plt .subplots (2 , 1 , sharex = True )
1011+ ax1 .plot (x , y1 , x , y2 , color = 'black' )
1012+ ax1 .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'white' , hatch = '/' ,
1013+ interpolate = True )
1014+ ax1 .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
1015+ interpolate = True )
10321016
10331017 # Test support for masked arrays.
10341018 y2 = np .ma .masked_greater (y2 , 1.0 )
10351019 # Test that plotting works for masked arrays with the first element masked
10361020 y2 [0 ] = np .ma .masked
1037- ax1 = fig .add_subplot (212 , sharex = ax )
1038- ax1 .plot (x , y1 , x , y2 , color = 'black' )
1039- ax1 .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'green' ,
1021+ ax2 .plot (x , y1 , x , y2 , color = 'black' )
1022+ ax2 .fill_between (x , y1 , y2 , where = y2 >= y1 , facecolor = 'green' ,
10401023 interpolate = True )
1041- ax1 .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
1024+ ax2 .fill_between (x , y1 , y2 , where = y2 <= y1 , facecolor = 'red' ,
10421025 interpolate = True )
10431026
10441027
@@ -1049,8 +1032,7 @@ def test_fill_between_interpolate_decreasing():
10491032 t = np .array ([9.4 , 7 , 2.2 ])
10501033 prof = np .array ([7.9 , 6.6 , 3.8 ])
10511034
1052- fig = plt .figure (figsize = (9 , 9 ))
1053- ax = fig .add_subplot (1 , 1 , 1 )
1035+ fig , ax = plt .subplots (figsize = (9 , 9 ))
10541036
10551037 ax .plot (t , p , 'tab:red' )
10561038 ax .plot (prof , p , 'k' )
@@ -1069,8 +1051,7 @@ def test_symlog():
10691051 x = np .array ([0 , 1 , 2 , 4 , 6 , 9 , 12 , 24 ])
10701052 y = np .array ([1000000 , 500000 , 100000 , 100 , 5 , 0 , 0 , 0 ])
10711053
1072- fig = plt .figure ()
1073- ax = fig .add_subplot (111 )
1054+ fig , ax = plt .subplots ()
10741055 ax .plot (x , y )
10751056 ax .set_yscale ('symlog' )
10761057 ax .set_xscale ('linear' )
@@ -1083,37 +1064,12 @@ def test_symlog2():
10831064 # Numbers from -50 to 50, with 0.1 as step
10841065 x = np .arange (- 50 , 50 , 0.001 )
10851066
1086- fig = plt .figure ()
1087- ax = fig .add_subplot (511 )
1088- # Plots a simple linear function 'f(x) = x'
1089- ax .plot (x , x )
1090- ax .set_xscale ('symlog' , linthreshx = 20.0 )
1091- ax .grid (True )
1092-
1093- ax = fig .add_subplot (512 )
1094- # Plots a simple linear function 'f(x) = x'
1095- ax .plot (x , x )
1096- ax .set_xscale ('symlog' , linthreshx = 2.0 )
1097- ax .grid (True )
1098-
1099- ax = fig .add_subplot (513 )
1100- # Plots a simple linear function 'f(x) = x'
1101- ax .plot (x , x )
1102- ax .set_xscale ('symlog' , linthreshx = 1.0 )
1103- ax .grid (True )
1104-
1105- ax = fig .add_subplot (514 )
1106- # Plots a simple linear function 'f(x) = x'
1107- ax .plot (x , x )
1108- ax .set_xscale ('symlog' , linthreshx = 0.1 )
1109- ax .grid (True )
1110-
1111- ax = fig .add_subplot (515 )
1112- # Plots a simple linear function 'f(x) = x'
1113- ax .plot (x , x )
1114- ax .set_xscale ('symlog' , linthreshx = 0.01 )
1115- ax .grid (True )
1116- ax .set_ylim (- 0.1 , 0.1 )
1067+ fig , axs = plt .subplots (5 , 1 )
1068+ for ax , linthreshx in zip (axs , [20. , 2. , 1. , 0.1 , 0.01 ]):
1069+ ax .plot (x , x )
1070+ ax .set_xscale ('symlog' , linthreshx = linthreshx )
1071+ ax .grid (True )
1072+ axs [- 1 ].set_ylim (- 0.1 , 0.1 )
11171073
11181074
11191075def test_pcolorargs_5205 ():
@@ -1145,15 +1101,10 @@ def test_pcolormesh():
11451101 # The color array can include masked values:
11461102 Zm = ma .masked_where (np .abs (Qz ) < 0.5 * np .max (Qz ), Z )
11471103
1148- fig = plt .figure ()
1149- ax = fig .add_subplot (131 )
1150- ax .pcolormesh (Qx , Qz , Z , lw = 0.5 , edgecolors = 'k' )
1151-
1152- ax = fig .add_subplot (132 )
1153- ax .pcolormesh (Qx , Qz , Z , lw = 2 , edgecolors = ['b' , 'w' ])
1154-
1155- ax = fig .add_subplot (133 )
1156- ax .pcolormesh (Qx , Qz , Z , shading = "gouraud" )
1104+ fig , (ax1 , ax2 , ax3 ) = plt .subplots (1 , 3 )
1105+ ax1 .pcolormesh (Qx , Qz , Z , lw = 0.5 , edgecolors = 'k' )
1106+ ax2 .pcolormesh (Qx , Qz , Z , lw = 2 , edgecolors = ['b' , 'w' ])
1107+ ax3 .pcolormesh (Qx , Qz , Z , shading = "gouraud" )
11571108
11581109
11591110@image_comparison (baseline_images = ['pcolormesh_datetime_axis' ],
0 commit comments