Skip to content

Commit 00dfd84

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 a4dca24 commit 00dfd84

File tree

12 files changed

+19
-22
lines changed

12 files changed

+19
-22
lines changed

galleries/examples/misc/custom_projection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def _get_core_transform(self, resolution):
439439
import matplotlib.pyplot as plt
440440

441441
# Now make a simple example using the custom projection.
442-
fig, ax = plt.subplots(subplot_kw={'projection': 'custom_hammer'})
442+
ax = plt.figure().add_subplot(projection="custom_hammer")
443443
ax.plot([-1, 1, 1], [-1, -1, 1], "o-")
444444
ax.grid()
445445

galleries/examples/mplot3d/custom_shaded_3d_surface.py

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

2626
# Set up plot
27-
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
27+
ax = plt.figure().add_subplot(projection="3d")
2828

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

galleries/examples/mplot3d/stem3d_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
y = np.sin(theta - np.pi/2)
1616
z = theta
1717

18-
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
18+
ax = plt.figure().add_subplot(projection="3d")
1919
ax.stem(x, y, z)
2020

2121
plt.show()
@@ -28,7 +28,7 @@
2828
# configurable via keyword arguments. For more advanced control adapt the line
2929
# objects returned by `~mpl_toolkits.mplot3d.axes3d.Axes3D.stem`.
3030

31-
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
31+
ax = plt.figure().add_subplot(projection="3d")
3232
markerline, stemlines, baseline = ax.stem(
3333
x, y, z, linefmt='grey', markerfmt='D', bottom=np.pi)
3434
markerline.set_markerfacecolor('none')
@@ -44,7 +44,7 @@
4444
# For examples, by setting ``orientation='x'``, the stems are projected along
4545
# the *x*-direction, and the baseline is in the *yz*-plane.
4646

47-
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
47+
ax = plt.figure().add_subplot(projection="3d")
4848
markerline, stemlines, baseline = ax.stem(x, y, z, bottom=-1, orientation='x')
4949
ax.set(xlabel='x', ylabel='y', zlabel='z')
5050

galleries/examples/pie_and_polar_charts/polar_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
r = np.arange(0, 2, 0.01)
1212
theta = 2 * np.pi * r
1313

14-
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
14+
ax = plt.figure().add_subplot(projection="polar")
1515
ax.plot(theta, r)
1616
ax.set_rmax(2)
1717
ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks

galleries/examples/shapes_and_collections/ellipse_demo.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
2222
angle=np.random.rand() * 360)
2323
for i in range(NUM)]
2424

25-
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
25+
fig, ax = plt.subplots()
26+
ax.set(xlim=(0, 10), ylim=(0, 10), aspect="equal")
27+
2628
for e in ells:
2729
ax.add_artist(e)
2830
e.set_clip_box(ax.bbox)
2931
e.set_alpha(np.random.rand())
3032
e.set_facecolor(np.random.rand(3))
3133

32-
ax.set_xlim(0, 10)
33-
ax.set_ylim(0, 10)
34-
3534
plt.show()
3635

3736
# %%
@@ -45,15 +44,13 @@
4544
angle_step = 45 # degrees
4645
angles = np.arange(0, 180, angle_step)
4746

48-
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
47+
fig, ax = plt.subplots()
48+
ax.set(xlim=(-2.2, 2.2), ylim=(-2.2, 2.2), aspect="equal")
4949

5050
for angle in angles:
5151
ellipse = Ellipse((0, 0), 4, 2, angle=angle, alpha=0.1)
5252
ax.add_artist(ellipse)
5353

54-
ax.set_xlim(-2.2, 2.2)
55-
ax.set_ylim(-2.2, 2.2)
56-
5754
plt.show()
5855

5956
# %%

galleries/examples/text_labels_and_annotations/annotation_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
# The text in the example is placed in the fractional figure coordinate system.
111111
# Text keyword arguments like horizontal and vertical alignment are respected.
112112

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

galleries/examples/widgets/lasso_selector_demo_sgskip.py

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

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

84-
subplot_kw = dict(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
85-
fig, ax = plt.subplots(subplot_kw=subplot_kw)
84+
fig, ax = plt.subplots()
85+
ax.set(xlim=(0, 1), ylim=(0, 1))
8686

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

galleries/plot_types/3D/scatter3d_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
zs = rng.uniform(-50, -25, n)
2020

2121
# Plot
22-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
22+
ax = plt.figure().add_subplot(projection="3d")
2323
ax.scatter(xs, ys, zs)
2424

2525
ax.set(xticklabels=[],

galleries/plot_types/3D/surface3d_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Z = np.sin(R)
2121

2222
# Plot the surface
23-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
23+
ax = plt.figure().add_subplot(projection="3d")
2424
ax.plot_surface(X, Y, Z, vmin=Z.min() * 2, cmap=cm.Blues)
2525

2626
ax.set(xticklabels=[],

galleries/plot_types/3D/trisurf3d_simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
z = np.sin(-x*y)
2626

2727
# Plot
28-
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
28+
ax = plt.figure().add_subplot(projection="3d")
2929
ax.plot_trisurf(x, y, z, vmin=z.min() * 2, cmap=cm.Blues)
3030

3131
ax.set(xticklabels=[],

0 commit comments

Comments
 (0)