Skip to content

Replace subplot(ijk) calls by subplots(i, j) #18567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/misc/patheffect_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from matplotlib import patheffects
import numpy as np

plt.figure(figsize=(8, 3))
ax1 = plt.subplot(131)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 3))
ax1.imshow([[1, 2], [2, 3]])
txt = ax1.annotate("test", (1., 1.), (0., 0),
arrowprops=dict(arrowstyle="->",
Expand All @@ -25,7 +24,6 @@
foreground="w")]
ax1.grid(True, linestyle="-", path_effects=pe)

ax2 = plt.subplot(132)
arr = np.arange(25).reshape((5, 5))
ax2.imshow(arr)
cntr = ax2.contour(arr, colors="k")
Expand All @@ -38,7 +36,6 @@
patheffects.withStroke(linewidth=3, foreground="w")])

# shadow as a path effect
ax3 = plt.subplot(133)
p1, = ax3.plot([0, 1], [0, 1])
leg = ax3.legend([p1], ["Line 1"], fancybox=True, loc='upper left')
leg.legendPatch.set_path_effects([patheffects.withSimplePatchShadow()])
Expand Down
4 changes: 1 addition & 3 deletions examples/pie_and_polar_charts/bar_of_pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import numpy as np

# make figure and assign axis objects
fig = plt.figure(figsize=(9, 5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
fig.subplots_adjust(wspace=0)

# pie chart parameters
Expand Down
24 changes: 8 additions & 16 deletions examples/text_labels_and_annotations/demo_text_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,24 @@ def draw(self, renderer=None):

usetex = plt.rcParams["text.usetex"]

fig = plt.figure()
fig, (ax1, ax2) = plt.subplots(2)

# EXAMPLE 1

ax = plt.subplot(211)

arr = plt.imread(get_sample_data("grace_hopper.png"))

text_path = TextPath((0, 0), "!?", size=150)
p = PathClippedImagePatch(text_path, arr, ec="k",
transform=IdentityTransform())

# p.set_clip_on(False)

# make offset box
offsetbox = AuxTransformBox(IdentityTransform())
offsetbox.add_artist(p)

# make anchored offset box
ao = AnchoredOffsetbox(loc='upper left', child=offsetbox, frameon=True,
borderpad=0.2)
ax.add_artist(ao)
ax1.add_artist(ao)

# another text
from matplotlib.patches import PathPatch
Expand All @@ -95,16 +91,13 @@ def draw(self, renderer=None):
box_alignment=(1., 0.),
frameon=False
)
ax.add_artist(ab)
ax1.add_artist(ab)

ax.imshow([[0, 1, 2], [1, 2, 3]], cmap=plt.cm.gist_gray_r,
interpolation="bilinear",
aspect="auto")
ax1.imshow([[0, 1, 2], [1, 2, 3]], cmap=plt.cm.gist_gray_r,
interpolation="bilinear", aspect="auto")

# EXAMPLE 2

ax = plt.subplot(212)

arr = np.arange(256).reshape(1, 256) / 256

if usetex:
Expand All @@ -131,11 +124,10 @@ def draw(self, renderer=None):
boxcoords="offset points",
box_alignment=(0.5, 0.5),
)
# text_path.set_size(10)

ax.add_artist(ab)
ax2.add_artist(ab)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax2.set_xlim(0, 1)
ax2.set_ylim(0, 1)

plt.show()
33 changes: 11 additions & 22 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,26 +602,21 @@ def test_fill_units():
dt = np.arange('2009-04-27', '2009-04-29', dtype='datetime64[D]')
dtn = mdates.date2num(dt)

fig = plt.figure()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of this idiom versus fig, axs = plt.subplots(2, 2). You lose the ability to iterate if you spell them all out like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think which is better depends on what you are doing (a grid of similar Axes or a grid of very different Axes).

In this case the code was previously using axN so this is the minimal touch to move away from add_subplot(XYZ).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I think it also depends on the names you use,

fig, (ax_hist, ax_map) = plt.subplots(2)

seems much better that slicing into an array (which is similar to the argument of why subplot_mosaic returns a dict).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @tacaswell, especially since this minimizes changes.


# Top-Left
ax1 = fig.add_subplot(221)
ax1.plot([t], [value], yunits='deg', color='red')
ind = [0, 0, 1, 1]
ax1.fill(dtn[ind], [0.0, 0.0, 90.0, 0.0], 'b')
# Top-Right
ax2 = fig.add_subplot(222)

ax2.plot([t], [value], yunits='deg', color='red')
ax2.fill([t, t, t + day, t + day],
[0.0, 0.0, 90.0, 0.0], 'b')
# Bottom-Left
ax3 = fig.add_subplot(223)

ax3.plot([t], [value], yunits='deg', color='red')
ax3.fill(dtn[ind],
[0 * units.deg, 0 * units.deg, 90 * units.deg, 0 * units.deg],
'b')
# Bottom-Right
ax4 = fig.add_subplot(224)

ax4.plot([t], [value], yunits='deg', color='red')
ax4.fill([t, t, t + day, t + day],
[0 * units.deg, 0 * units.deg, 90 * units.deg, 0 * units.deg],
Expand All @@ -635,22 +630,16 @@ def test_single_point():
matplotlib.rcParams['lines.marker'] = 'o'
matplotlib.rcParams['axes.grid'] = True

plt.figure()
plt.subplot(211)
plt.plot([0], [0], 'o')

plt.subplot(212)
plt.plot([1], [1], 'o')
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot([0], [0], 'o')
ax2.plot([1], [1], 'o')

# Reuse testcase from above for a labeled data test
data = {'a': [0], 'b': [1]}

plt.figure()
plt.subplot(211)
plt.plot('a', 'a', 'o', data=data)

plt.subplot(212)
plt.plot('b', 'b', 'o', data=data)
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot('a', 'a', 'o', data=data)
ax2.plot('b', 'b', 'o', data=data)


@image_comparison(['single_date.png'], style='mpl20')
Expand Down Expand Up @@ -721,7 +710,7 @@ def test_axvspan_epoch():
dt = units.Duration("ET", units.day.convert("sec"))

ax = plt.gca()
plt.axvspan(t0, tf, facecolor="blue", alpha=0.25)
ax.axvspan(t0, tf, facecolor="blue", alpha=0.25)
ax.set_xlim(t0 - 5.0*dt, tf + 5.0*dt)


Expand Down
5 changes: 1 addition & 4 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,8 @@ def test_bbox_inches():
'pgf.rcfonts': False}
mpl.rcParams.update(rc_xelatex)

Y, X = np.ogrid[-1:1:40j, -1:1:40j]
fig = plt.figure()
ax1 = fig.add_subplot(121)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(range(5))
ax2 = fig.add_subplot(122)
ax2.plot(range(5))
plt.tight_layout()

Expand Down
5 changes: 1 addition & 4 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,8 @@ def test_contourf_log_extension():
plt.rcParams['pcolormesh.snap'] = False

# Test that contourf with lognorm is extended correctly
fig = plt.figure(figsize=(10, 5))
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 5))
fig.subplots_adjust(left=0.05, right=0.95)
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)

# make data set with large range e.g. between 1e-8 and 1e10
data_exp = np.linspace(-7.5, 9.5, 1200)
Expand Down
12 changes: 3 additions & 9 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@ def test_image_interps():
# Remove this line when this test image is regenerated.
plt.rcParams['text.kerning_factor'] = 6

X = np.arange(100)
X = X.reshape(5, 20)
X = np.arange(100).reshape(5, 20)

fig = plt.figure()
ax1 = fig.add_subplot(311)
fig, (ax1, ax2, ax3) = plt.subplots(3)
ax1.imshow(X, interpolation='nearest')
ax1.set_title('three interpolations')
ax1.set_ylabel('nearest')

ax2 = fig.add_subplot(312)
ax2.imshow(X, interpolation='bilinear')
ax2.set_ylabel('bilinear')

ax3 = fig.add_subplot(313)
ax3.imshow(X, interpolation='bicubic')
ax3.set_ylabel('bicubic')

Expand Down Expand Up @@ -69,11 +65,9 @@ def test_interp_nearest_vs_none():
rcParams['savefig.dpi'] = 3
X = np.array([[[218, 165, 32], [122, 103, 238]],
[[127, 255, 0], [255, 99, 71]]], dtype=np.uint8)
fig = plt.figure()
ax1 = fig.add_subplot(121)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(X, interpolation='none')
ax1.set_title('interpolation none')
ax2 = fig.add_subplot(122)
ax2.imshow(X, interpolation='nearest')
ax2.set_title('interpolation nearest')

Expand Down
2 changes: 1 addition & 1 deletion tutorials/text/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@

This allows annotating a point in another axes::

ax1, ax2 = subplot(121), subplot(122)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax2.annotate("Test", xy=(0.5, 0.5), xycoords=ax1.transData,
xytext=(0.5, 0.5), textcoords=ax2.transData,
arrowprops=dict(arrowstyle="->"))
Expand Down