Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 365d54c

Browse filesBrowse files
committed
Use plt.subplots()
1 parent aae55fc commit 365d54c
Copy full SHA for 365d54c
Expand file treeCollapse file tree

23 files changed

+85
-195
lines changed

‎examples/lines_bars_and_markers/simple_plot.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/simple_plot.py
-2Lines 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

Copy file name to clipboardExpand all lines: examples/misc/load_converter.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
converters={0: bytespdate2num('%d-%b-%y')},
1818
skiprows=1, usecols=(0, 2), unpack=True)
1919

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

‎examples/pyplots/annotate_transform.py

Copy file name to clipboardExpand all lines: examples/pyplots/annotate_transform.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
x = np.arange(0, 10, 0.005)
1414
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
1515

16-
fig = plt.figure()
17-
ax = fig.add_subplot(111)
16+
fig, ax = plt.subplots()
1817
ax.plot(x, y)
1918
ax.set_xlim(0, 10)
2019
ax.set_ylim(-1, 1)

‎examples/pyplots/auto_subplots_adjust.py

Copy file name to clipboardExpand all lines: examples/pyplots/auto_subplots_adjust.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"""
1414
import matplotlib.pyplot as plt
1515
import matplotlib.transforms as mtransforms
16-
fig = plt.figure()
17-
ax = fig.add_subplot(111)
16+
fig, ax = plt.subplots()
1817
ax.plot(range(10))
1918
ax.set_yticks((2,5,7))
2019
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

‎examples/ticks_and_spines/tick_labels_from_values.py

Copy file name to clipboardExpand all lines: examples/ticks_and_spines/tick_labels_from_values.py
+1-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/lines.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,8 +1372,7 @@ def process_selected(self, ind, xs, ys):
13721372
self.markers.set_data(xs, ys)
13731373
self.canvas.draw()
13741374
1375-
fig = plt.figure()
1376-
ax = fig.add_subplot(111)
1375+
fig, ax = plt.subplots()
13771376
x, y = np.random.rand(2, 30)
13781377
line, = ax.plot(x, y, 'bs-', picker=5)
13791378

‎lib/matplotlib/mlab.py

Copy file name to clipboardExpand all lines: lib/matplotlib/mlab.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3876,8 +3876,7 @@ def cross_from_below(x, threshold):
38763876
t = np.arange(0.0, 2.0, 0.1)
38773877
s = np.sin(2*np.pi*t)
38783878
3879-
fig = plt.figure()
3880-
ax = fig.add_subplot(111)
3879+
fig, ax = plt.subplots()
38813880
ax.plot(t, s, '-o')
38823881
ax.axhline(0.5)
38833882
ax.axhline(-0.5)

‎lib/matplotlib/tests/test_artist.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_artist.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ def test_cull_markers():
129129
x = np.random.random(20000)
130130
y = np.random.random(20000)
131131

132-
fig = plt.figure()
133-
ax = fig.add_subplot(111)
132+
fig, ax = plt.subplots()
134133
ax.plot(x, y, 'k.')
135134
ax.set_xlim(2, 3)
136135

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+34-83Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,7 @@ def test_axhspan_epoch():
821821
remove_text=True, extensions=['png'])
822822
def 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'])
868863
def 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

881875
def 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
)
993980
def 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
)
1011997
def 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

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

‎lib/matplotlib/tests/test_backend_svg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_svg.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919

2020
def test_visibility():
21-
fig = plt.figure()
22-
ax = fig.add_subplot(111)
21+
fig, ax = plt.subplots()
2322

2423
x = np.linspace(0, 4 * np.pi, 50)
2524
y = np.sin(x)

‎lib/matplotlib/tests/test_backends_interactive.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backends_interactive.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def _get_testable_interactive_backends():
4949
"webagg.port_retries": 1,
5050
})
5151
52-
fig = plt.figure()
53-
ax = fig.add_subplot(111)
52+
fig, ax = plt.subplots()
5453
ax.plot([0, 1], [2, 3])
5554
5655
if rcParams["backend"].startswith("GTK3"):

‎lib/matplotlib/tests/test_colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colorbar.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ def test_remove_from_figure(use_gridspec):
210210
"""
211211
Test `remove_from_figure` with the specified ``use_gridspec`` setting
212212
"""
213-
fig = plt.figure()
214-
ax = fig.add_subplot(111)
213+
fig, ax = plt.subplots()
215214
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
216215
sc.set_array(np.array([5, 6]))
217216
pre_figbox = np.array(ax.figbox)

‎lib/matplotlib/tests/test_constrainedlayout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_constrainedlayout.py
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ def test_constrained_layout9():
163163
'Test for handling suptitle and for sharex and sharey'
164164
fig, axs = plt.subplots(2, 2, constrained_layout=True,
165165
sharex=False, sharey=False)
166-
# ax = fig.add_subplot(111)
167166
for ax in axs.flatten():
168167
pcm = example_pcolor(ax, fontsize=24)
169168
ax.set_xlabel('')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.