Skip to content

Commit fa8ef97

Browse files
committed
Prefer add_subplot(foo=bar) to subplots(subplot_kw={"foo": bar}).
When creating a single subplot I think the former is more readable. (Note that before mpl3.1 one had to write `add_subplot(111, foo=bar)` where the tradeoff was less clear.)
1 parent 4b50ce6 commit fa8ef97

File tree

11 files changed

+31
-31
lines changed

11 files changed

+31
-31
lines changed

examples/frontpage/3D.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
region = np.s_[5:50, 5:50]
2424
x, y, z = x[region], y[region], z[region]
2525

26-
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
26+
fig = plt.figure()
27+
ax = fig.add_subplot(projection='3d')
2728

2829
ls = LightSource(270, 45)
2930
# To use a custom hillshading mode, override the built-in shading and pass

examples/mplot3d/custom_shaded_3d_surface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
x, y, z = x[region], y[region], z[region]
2626

2727
# Set up plot
28-
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
28+
ax = plt.figure().add_subplot(projection='3d')
2929

3030
ls = LightSource(270, 45)
3131
# To use a custom hillshading mode, override the built-in shading and pass

examples/mplot3d/wire3d_zero_stride.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import matplotlib.pyplot as plt
1212

1313

14-
fig, [ax1, ax2] = plt.subplots(2, 1, figsize=(8, 12), subplot_kw={'projection': '3d'})
14+
fig, (ax1, ax2) = plt.subplots(
15+
2, 1, figsize=(8, 12), subplot_kw={'projection': '3d'})
1516

1617
# Get the test data
1718
X, Y, Z = axes3d.get_test_data(0.05)

examples/pie_and_polar_charts/nested_pie.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# a circle. The cumulative sum of the values are used as the edges
5353
# of the bars.
5454

55-
fig, ax = plt.subplots(subplot_kw=dict(polar=True))
55+
ax = plt.figure().add_subplot(polar=True)
5656

5757
size = 0.3
5858
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])

examples/pie_and_polar_charts/pie_and_donut_labels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import numpy as np
3434
import matplotlib.pyplot as plt
3535

36-
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
36+
ax = plt.figure(figsize=(6, 3)).add_subplot(aspect="equal")
3737

3838
recipe = ["375 g flour",
3939
"75 g sugar",
@@ -88,7 +88,7 @@ def func(pct, allvals):
8888
# determined parameters.
8989

9090

91-
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
91+
ax = plt.figure(figsize=(6, 3)).add_subplot(aspect="equal")
9292

9393
recipe = ["225 g flour",
9494
"90 g sugar",

examples/shapes_and_collections/ellipse_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
angle=np.random.rand() * 360)
1919
for i in range(NUM)]
2020

21-
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
21+
ax = plt.figure().add_subplot(aspect='equal')
2222
for e in ells:
2323
ax.add_artist(e)
2424
e.set_clip_box(ax.bbox)

examples/text_labels_and_annotations/annotation_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
# The text in the example is placed in the fractional figure coordinate system.
110110
# Text keyword args like horizontal and vertical alignment are respected.
111111

112-
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'), figsize=(3, 3))
112+
ax = plt.figure(figsize=(3, 3)).add_subplot(projection='polar')
113113
r = np.arange(0, 1, 0.001)
114114
theta = 2*2*np.pi*r
115115
line, = ax.plot(theta, r)
@@ -131,7 +131,7 @@
131131

132132
el = Ellipse((0, 0), 10, 20, facecolor='r', alpha=0.5)
133133

134-
fig, ax = plt.subplots(subplot_kw=dict(aspect='equal'))
134+
ax = plt.figure().add_subplot(aspect='equal')
135135
ax.add_artist(el)
136136
el.set_clip_box(ax.bbox)
137137
ax.annotate('the top',

examples/widgets/lasso_selector_demo_sgskip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def disconnect(self):
8282

8383
data = np.random.rand(100, 2)
8484

85-
subplot_kw = dict(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
86-
fig, ax = plt.subplots(subplot_kw=subplot_kw)
85+
fig = plt.figure()
86+
ax = fig.add_subplot(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
8787

8888
pts = ax.scatter(data[:, 0], data[:, 1], s=80)
8989
selector = SelectFromCollection(ax, pts)

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,21 +769,21 @@ def test_polar_theta_limits():
769769

770770
@check_figures_equal(extensions=["png"])
771771
def test_polar_rlim(fig_test, fig_ref):
772-
ax = fig_test.subplots(subplot_kw={'polar': True})
772+
ax = fig_test.add_subplot(polar=True)
773773
ax.set_rlim(top=10)
774774
ax.set_rlim(bottom=.5)
775775

776-
ax = fig_ref.subplots(subplot_kw={'polar': True})
776+
ax = fig_ref.add_subplot(polar=True)
777777
ax.set_rmax(10.)
778778
ax.set_rmin(.5)
779779

780780

781781
@check_figures_equal(extensions=["png"])
782782
def test_polar_rlim_bottom(fig_test, fig_ref):
783-
ax = fig_test.subplots(subplot_kw={'polar': True})
783+
ax = fig_test.add_subplot(polar=True)
784784
ax.set_rlim(bottom=[.5, 10])
785785

786-
ax = fig_ref.subplots(subplot_kw={'polar': True})
786+
ax = fig_ref.add_subplot(polar=True)
787787
ax.set_rmax(10.)
788788
ax.set_rmin(.5)
789789

@@ -6275,7 +6275,8 @@ def test_minor_accountedfor():
62756275

62766276

62776277
def test_get_tightbbox_polar():
6278-
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
6278+
fig = plt.figure()
6279+
ax = fig.add_subplot(projection='polar')
62796280
fig.canvas.draw()
62806281
bb = ax.get_tightbbox(fig.canvas.get_renderer())
62816282
assert_allclose(bb.extents,

lib/matplotlib/tests/test_quiver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_angles_and_scale():
199199
@image_comparison(['quiver_xy.png'], remove_text=True)
200200
def test_quiver_xy():
201201
# simple arrow pointing from SW to NE
202-
fig, ax = plt.subplots(subplot_kw=dict(aspect='equal'))
202+
ax = plt.figure().add_subplot(aspect='equal')
203203
ax.quiver(0, 0, 1, 1, angles='xy', scale_units='xy', scale=1)
204204
ax.set_xlim(0, 1.1)
205205
ax.set_ylim(0, 1.1)

0 commit comments

Comments
 (0)