Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86a3bac

Browse files
committed
Merge pull request matplotlib#11580 from timhoffm/subplots
DOC: Use plt.subplots() Conflicts: lib/matplotlib/tests/test_backends_interactive.py - only backported the minimal change tutorials/advanced/transforms_tutorial.py - only backported the minimal change tutorials/text/pgf.py - only backported the minimal change
1 parent 6133a7d commit 86a3bac

22 files changed

+86
-192
lines changed

examples/lines_bars_and_markers/simple_plot.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
t = np.arange(0.0, 2.0, 0.01)
1515
s = 1 + np.sin(2 * np.pi * t)
1616

17-
# Note that using plt.subplots below is equivalent to using
18-
# fig = plt.figure() and then ax = fig.add_subplot(111)
1917
fig, ax = plt.subplots()
2018
ax.plot(t, s)
2119

examples/misc/load_converter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
converters={0: bytespdate2num('%d-%b-%y')},
1919
skiprows=1, usecols=(0, 2), unpack=True)
2020

21-
fig = plt.figure()
22-
ax = fig.add_subplot(111)
21+
fig, ax = plt.subplots()
2322
ax.plot_date(dates, closes, '-')
2423
fig.autofmt_xdate()
2524
plt.show()

examples/pyplots/annotate_transform.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
x = np.arange(0, 10, 0.005)
1111
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
1212

13-
fig = plt.figure()
14-
ax = fig.add_subplot(111)
13+
fig, ax = plt.subplots()
1514
ax.plot(x, y)
1615
ax.set_xlim(0, 10)
1716
ax.set_ylim(-1, 1)

examples/pyplots/auto_subplots_adjust.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"""
77
import matplotlib.pyplot as plt
88
import matplotlib.transforms as mtransforms
9-
fig = plt.figure()
10-
ax = fig.add_subplot(111)
9+
fig, ax = plt.subplots()
1110
ax.plot(range(10))
1211
ax.set_yticks((2,5,7))
1312
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

examples/ticks_and_spines/tick_labels_from_values.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import matplotlib.pyplot as plt
1919
from matplotlib.ticker import FuncFormatter, MaxNLocator
20-
fig = plt.figure()
21-
ax = fig.add_subplot(111)
20+
fig, ax = plt.subplots()
2221
xs = range(26)
2322
ys = range(26)
2423
labels = list('abcdefghijklmnopqrstuvwxyz')

lib/matplotlib/lines.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,8 +1446,7 @@ def process_selected(self, ind, xs, ys):
14461446
self.markers.set_data(xs, ys)
14471447
self.canvas.draw()
14481448
1449-
fig = plt.figure()
1450-
ax = fig.add_subplot(111)
1449+
fig, ax = plt.subplots()
14511450
x, y = np.random.rand(2, 30)
14521451
line, = ax.plot(x, y, 'bs-', picker=5)
14531452

lib/matplotlib/mlab.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,8 +3898,7 @@ def cross_from_below(x, threshold):
38983898
t = np.arange(0.0, 2.0, 0.1)
38993899
s = np.sin(2*np.pi*t)
39003900
3901-
fig = plt.figure()
3902-
ax = fig.add_subplot(111)
3901+
fig, ax = plt.subplots()
39033902
ax.plot(t, s, '-o')
39043903
ax.axhline(0.5)
39053904
ax.axhline(-0.5)

lib/matplotlib/tests/test_artist.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ def test_cull_markers():
131131
x = np.random.random(20000)
132132
y = np.random.random(20000)
133133

134-
fig = plt.figure()
135-
ax = fig.add_subplot(111)
134+
fig, ax = plt.subplots()
136135
ax.plot(x, y, 'k.')
137136
ax.set_xlim(2, 3)
138137

lib/matplotlib/tests/test_axes.py

Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,7 @@ def test_axhspan_epoch():
815815
remove_text=True, extensions=['png'])
816816
def 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'])
862857
def 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

875869
def 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
)
990977
def 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
)
1008994
def 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

11161072
def 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'],

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323

2424
def test_visibility():
25-
fig = plt.figure()
26-
ax = fig.add_subplot(111)
25+
fig, ax = plt.subplots()
2726

2827
x = np.linspace(0, 4 * np.pi, 50)
2928
y = np.sin(x)

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def _get_testable_interactive_backends():
4040
import sys
4141
from matplotlib import pyplot as plt
4242
43-
fig = plt.figure()
44-
ax = fig.add_subplot(111)
45-
ax.plot([1,2,3], [1,3,1])
43+
fig, ax = plt.subplots()
44+
ax.plot([0, 1], [2, 3])
45+
4646
fig.canvas.mpl_connect("draw_event", lambda event: sys.exit())
4747
plt.show()
4848
"""

lib/matplotlib/tests/test_colorbar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ def test_remove_from_figure(use_gridspec):
212212
"""
213213
Test `remove_from_figure` with the specified ``use_gridspec`` setting
214214
"""
215-
fig = plt.figure()
216-
ax = fig.add_subplot(111)
215+
fig, ax = plt.subplots()
217216
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
218217
sc.set_array(np.array([5, 6]))
219218
pre_figbox = np.array(ax.figbox)

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ def test_constrained_layout9():
212212
'Test for handling suptitle and for sharex and sharey'
213213
fig, axs = plt.subplots(2, 2, constrained_layout=True,
214214
sharex=False, sharey=False)
215-
# ax = fig.add_subplot(111)
216215
for ax in axs.flatten():
217216
pcm = example_pcolor(ax, fontsize=24)
218217
ax.set_xlabel('')

0 commit comments

Comments
 (0)