@@ -821,9 +821,7 @@ def test_axhspan_epoch():
821
821
remove_text = True , extensions = ['png' ])
822
822
def test_hexbin_extent ():
823
823
# this test exposes sf bug 2856228
824
- fig = plt .figure ()
825
-
826
- ax = fig .add_subplot (111 )
824
+ fig , ax = plt .subplots ()
827
825
data = (np .arange (2000 ) / 2000 ).reshape ((2 , 1000 ))
828
826
x , y = data
829
827
@@ -832,8 +830,7 @@ def test_hexbin_extent():
832
830
# Reuse testcase from above for a labeled data test
833
831
data = {"x" : x , "y" : y }
834
832
835
- fig = plt .figure ()
836
- ax = fig .add_subplot (111 )
833
+ fig , ax = plt .subplots ()
837
834
ax .hexbin ("x" , "y" , extent = [.1 , .3 , .6 , .7 ], data = data )
838
835
839
836
@@ -852,9 +849,7 @@ def __init__(self, x, y):
852
849
self .x = x
853
850
self .y = y
854
851
855
- fig = plt .figure ()
856
-
857
- ax = fig .add_subplot (111 )
852
+ fig , ax = plt .subplots ()
858
853
data = (np .arange (200 ) / 200 ).reshape ((2 , 100 ))
859
854
x , y = data
860
855
hb = ax .hexbin (x , y , extent = [.1 , .3 , .6 , .7 ], picker = - 1 )
@@ -867,32 +862,29 @@ def __init__(self, x, y):
867
862
extensions = ['png' ])
868
863
def test_hexbin_log ():
869
864
# Issue #1636
870
- fig = plt .figure ()
871
-
872
865
np .random .seed (0 )
873
866
n = 100000
874
867
x = np .random .standard_normal (n )
875
868
y = 2.0 + 3.0 * x + 4.0 * np .random .standard_normal (n )
876
869
y = np .power (2 , y * 0.5 )
877
- ax = fig .add_subplot (111 )
870
+
871
+ fig , ax = plt .subplots ()
878
872
ax .hexbin (x , y , yscale = 'log' )
879
873
880
874
881
875
def test_inverted_limits ():
882
876
# Test gh:1553
883
877
# Calling invert_xaxis prior to plotting should not disable autoscaling
884
878
# while still maintaining the inverted direction
885
- fig = plt .figure ()
886
- ax = fig .gca ()
879
+ fig , ax = plt .subplots ()
887
880
ax .invert_xaxis ()
888
881
ax .plot ([- 5 , - 3 , 2 , 4 ], [1 , 2 , - 3 , 5 ])
889
882
890
883
assert ax .get_xlim () == (4 , - 5 )
891
884
assert ax .get_ylim () == (- 3 , 5 )
892
885
plt .close ()
893
886
894
- fig = plt .figure ()
895
- ax = fig .gca ()
887
+ fig , ax = plt .subplots ()
896
888
ax .invert_yaxis ()
897
889
ax .plot ([- 5 , - 3 , 2 , 4 ], [1 , 2 , - 3 , 5 ])
898
890
@@ -908,8 +900,7 @@ def test_nonfinite_limits():
908
900
with np .errstate (divide = 'ignore' ):
909
901
y = np .log (x )
910
902
x [len (x )// 2 ] = np .nan
911
- fig = plt .figure ()
912
- ax = fig .add_subplot (111 )
903
+ fig , ax = plt .subplots ()
913
904
ax .plot (x , y )
914
905
915
906
@@ -924,9 +915,7 @@ def test_imshow():
924
915
r = np .sqrt (x ** 2 + y ** 2 - x * y )
925
916
926
917
# 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 ()
930
919
ax .imshow (r )
931
920
932
921
# Reuse testcase from above for a labeled data test
@@ -948,8 +937,7 @@ def test_imshow_clip():
948
937
r = np .sqrt (x ** 2 + y ** 2 - x * y )
949
938
950
939
# 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 ()
953
941
954
942
c = ax .contour (r , [N / 4 ])
955
943
x = c .collections [0 ]
@@ -970,8 +958,7 @@ def test_polycollection_joinstyle():
970
958
971
959
from matplotlib import collections as mcoll
972
960
973
- fig = plt .figure ()
974
- ax = fig .add_subplot (111 )
961
+ fig , ax = plt .subplots ()
975
962
verts = np .array ([[1 , 1 ], [1 , 2 ], [2 , 2 ], [2 , 1 ]])
976
963
c = mcoll .PolyCollection ([verts ], linewidths = 40 )
977
964
ax .add_collection (c )
@@ -991,8 +978,7 @@ def test_polycollection_joinstyle():
991
978
]
992
979
)
993
980
def test_fill_between_input (x , y1 , y2 ):
994
- fig = plt .figure ()
995
- ax = fig .add_subplot (211 )
981
+ fig , ax = plt .subplots ()
996
982
with pytest .raises (ValueError ):
997
983
ax .fill_between (x , y1 , y2 )
998
984
@@ -1009,8 +995,7 @@ def test_fill_between_input(x, y1, y2):
1009
995
]
1010
996
)
1011
997
def test_fill_betweenx_input (y , x1 , x2 ):
1012
- fig = plt .figure ()
1013
- ax = fig .add_subplot (211 )
998
+ fig , ax = plt .subplots ()
1014
999
with pytest .raises (ValueError ):
1015
1000
ax .fill_betweenx (y , x1 , x2 )
1016
1001
@@ -1022,23 +1007,21 @@ def test_fill_between_interpolate():
1022
1007
y1 = np .sin (2 * np .pi * x )
1023
1008
y2 = 1.2 * np .sin (4 * np .pi * x )
1024
1009
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 )
1032
1016
1033
1017
# Test support for masked arrays.
1034
1018
y2 = np .ma .masked_greater (y2 , 1.0 )
1035
1019
# Test that plotting works for masked arrays with the first element masked
1036
1020
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' ,
1040
1023
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' ,
1042
1025
interpolate = True )
1043
1026
1044
1027
@@ -1049,8 +1032,7 @@ def test_fill_between_interpolate_decreasing():
1049
1032
t = np .array ([9.4 , 7 , 2.2 ])
1050
1033
prof = np .array ([7.9 , 6.6 , 3.8 ])
1051
1034
1052
- fig = plt .figure (figsize = (9 , 9 ))
1053
- ax = fig .add_subplot (1 , 1 , 1 )
1035
+ fig , ax = plt .subplots (figsize = (9 , 9 ))
1054
1036
1055
1037
ax .plot (t , p , 'tab:red' )
1056
1038
ax .plot (prof , p , 'k' )
@@ -1069,8 +1051,7 @@ def test_symlog():
1069
1051
x = np .array ([0 , 1 , 2 , 4 , 6 , 9 , 12 , 24 ])
1070
1052
y = np .array ([1000000 , 500000 , 100000 , 100 , 5 , 0 , 0 , 0 ])
1071
1053
1072
- fig = plt .figure ()
1073
- ax = fig .add_subplot (111 )
1054
+ fig , ax = plt .subplots ()
1074
1055
ax .plot (x , y )
1075
1056
ax .set_yscale ('symlog' )
1076
1057
ax .set_xscale ('linear' )
@@ -1083,37 +1064,12 @@ def test_symlog2():
1083
1064
# Numbers from -50 to 50, with 0.1 as step
1084
1065
x = np .arange (- 50 , 50 , 0.001 )
1085
1066
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 )
1117
1073
1118
1074
1119
1075
def test_pcolorargs_5205 ():
@@ -1145,15 +1101,10 @@ def test_pcolormesh():
1145
1101
# The color array can include masked values:
F844
tr>1146
1102
Zm = ma .masked_where (np .abs (Qz ) < 0.5 * np .max (Qz ), Z )
1147
1103
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" )
1157
1108
1158
1109
1159
1110
@image_comparison (baseline_images = ['pcolormesh_datetime_axis' ],
0 commit comments