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

+2-1
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

+1-1
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

+2-1
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

+1-1
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

+2-2
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

+1-1
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

+2-2
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

+2-2
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

+6-5
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

+1-1
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)

lib/mpl_toolkits/tests/test_mplot3d.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ def test_bar3d_shaded():
4040

4141
views = [(-60, 30), (30, 30), (30, -30), (120, -30)]
4242
fig = plt.figure(figsize=plt.figaspect(1 / len(views)))
43-
axs = fig.subplots(
44-
1, len(views),
45-
subplot_kw=dict(projection='3d')
46-
)
43+
axs = fig.subplots(1, len(views), subplot_kw=dict(projection='3d'))
4744
for ax, (azim, elev) in zip(axs, views):
4845
ax.bar3d(x2d, y2d, x2d * 0, 1, 1, z, shade=True)
4946
ax.view_init(azim=azim, elev=elev)
@@ -612,7 +609,7 @@ def test_world():
612609
@image_comparison(['proj3d_lines_dists.png'],
613610
remove_text=True, style='default')
614611
def test_lines_dists():
615-
fig, ax = plt.subplots(figsize=(4, 6), subplot_kw=dict(aspect='equal'))
612+
ax = plt.figure(figsize=(4, 6)).add_subplot(aspect='equal')
616613

617614
xs = (0, 30)
618615
ys = (20, 150)
@@ -634,7 +631,7 @@ def test_lines_dists():
634631

635632

636633
def test_autoscale():
637-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
634+
ax = plt.figure().add_subplot(projection="3d")
638635
ax.margins(x=0, y=.1, z=.2)
639636
ax.plot([0, 1], [0, 1], [0, 1])
640637
assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.2, 1.2)
@@ -671,7 +668,7 @@ def test_invalid_axes_limits(setter, side, value):
671668
class TestVoxels:
672669
@image_comparison(['voxels-simple.png'], remove_text=True)
673670
def test_simple(self):
674-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
671+
ax = plt.figure().add_subplot(projection="3d")
675672

676673
x, y, z = np.indices((5, 4, 3))
677674
voxels = (x == y) | (y == z)
@@ -680,7 +677,7 @@ def test_simple(self):
680677
@image_comparison(['voxels-edge-style.png'],
681678
remove_text=True, style='default')
682679
def test_edge_style(self):
683-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
680+
ax = plt.figure().add_subplot(projection="3d")
684681

685682
x, y, z = np.indices((5, 5, 4))
686683
voxels = ((x - 2)**2 + (y - 2)**2 + (z-1.5)**2) < 2.2**2
@@ -692,7 +689,7 @@ def test_edge_style(self):
692689
@image_comparison(['voxels-named-colors.png'], remove_text=True)
693690
def test_named_colors(self):
694691
"""Test with colors set to a 3d object array of strings."""
695-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
692+
ax = plt.figure().add_subplot(projection="3d")
696693

697694
x, y, z = np.indices((10, 10, 10))
698695
voxels = (x == y) | (y == z)
@@ -705,7 +702,7 @@ def test_named_colors(self):
705702
@image_comparison(['voxels-rgb-data.png'], remove_text=True)
706703
def test_rgb_data(self):
707704
"""Test with colors set to a 4d float array of rgb data."""
708-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
705+
ax = plt.figure().add_subplot(projection="3d")
709706

710707
x, y, z = np.indices((10, 10, 10))
711708
voxels = (x == y) | (y == z)
@@ -717,7 +714,7 @@ def test_rgb_data(self):
717714

718715
@image_comparison(['voxels-alpha.png'], remove_text=True)
719716
def test_alpha(self):
720-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
717+
ax = plt.figure().add_subplot(projection="3d")
721718

722719
x, y, z = np.indices((10, 10, 10))
723720
v1 = x == y
@@ -735,7 +732,7 @@ def test_alpha(self):
735732

736733
@image_comparison(['voxels-xyz.png'], tol=0.01)
737734
def test_xyz(self):
738-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
735+
ax = plt.figure().add_subplot(projection="3d")
739736

740737
def midpoints(x):
741738
sl = ()
@@ -770,7 +767,7 @@ def test_calling_conventions(self):
770767
x, y, z = np.indices((3, 4, 5))
771768
filled = np.ones((2, 3, 4))
772769

773-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
770+
ax = plt.figure().add_subplot(projection="3d")
774771

775772
# all the valid calling conventions
776773
for kw in (dict(), dict(edgecolor='k')):
@@ -808,7 +805,7 @@ def test_line3d_set_get_data_3d():
808805
def test_inverted_cla():
809806
# Github PR #5450. Setting autoscale should reset
810807
# axes to be non-inverted.
811-
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
808+
ax = plt.figure().add_subplot(projection="3d")
812809
# 1. test that a new axis is not inverted per default
813810
assert not ax.xaxis_inverted()
814811
assert not ax.yaxis_inverted()

0 commit comments

Comments
 (0)