Skip to content

Commit dc043e1

Browse files
authored
Merge pull request #14079 from timhoffm/subplots-flat
Consistently use axs.flat instead of axs.flatten()
2 parents 3a125b1 + 93576b0 commit dc043e1

File tree

11 files changed

+51
-53
lines changed

11 files changed

+51
-53
lines changed

examples/specialty_plots/radar_chart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def example_data():
168168

169169
colors = ['b', 'r', 'g', 'm', 'y']
170170
# Plot the four cases from the example data on separate axes
171-
for ax, (title, case_data) in zip(axes.flatten(), data):
171+
for ax, (title, case_data) in zip(axes.flat, data):
172172
ax.set_rgrids([0.2, 0.4, 0.6, 0.8])
173173
ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
174174
horizontalalignment='center', verticalalignment='center')

examples/statistics/boxplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
axes[1, 2].boxplot(data, labels=labels, showfliers=False)
4949
axes[1, 2].set_title('showfliers=False', fontsize=fs)
5050

51-
for ax in axes.flatten():
51+
for ax in axes.flat:
5252
ax.set_yscale('log')
5353
ax.set_yticklabels([])
5454

@@ -88,7 +88,7 @@
8888
axes[1, 2].boxplot(data, whis=[15, 85])
8989
axes[1, 2].set_title('whis=[15, 85]\n#percentiles', fontsize=fs)
9090

91-
for ax in axes.flatten():
91+
for ax in axes.flat:
9292
ax.set_yscale('log')
9393
ax.set_yticklabels([])
9494

examples/statistics/bxp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
axes[1, 2].bxp(stats, showfliers=False)
6363
axes[1, 2].set_title('showfliers=False', fontsize=fs)
6464

65-
for ax in axes.flatten():
65+
for ax in axes.flat:
6666
ax.set_yscale('log')
6767
ax.set_yticklabels([])
6868

@@ -95,7 +95,7 @@
9595
showmeans=True)
9696
axes[1, 1].set_title('Custom mean\nas line', fontsize=fs)
9797

98-
for ax in axes.flatten():
98+
for ax in axes.flat:
9999
ax.set_yscale('log')
100100
ax.set_yticklabels([])
101101

examples/statistics/violinplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
bw_method=0.5)
5858
axes[1, 2].set_title('Custom violinplot 6', fontsize=fs)
5959

60-
for ax in axes.flatten():
60+
for ax in axes.flat:
6161
ax.set_yticklabels([])
6262

6363
fig.suptitle("Violin Plotting Examples")

examples/subplots_axes_and_figures/demo_constrained_layout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def example_plot(ax):
2727

2828
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=False)
2929

30-
for ax in axs.flatten():
30+
for ax in axs.flat:
3131
example_plot(ax)
3232

3333
###############################################################################
3434
# adding ``constrained_layout=True`` automatically adjusts.
3535

3636
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)
3737

38-
for ax in axs.flatten():
38+
for ax in axs.flat:
3939
example_plot(ax)
4040

4141
###############################################################################

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,10 @@ def test_minorticks_on_rcParams_both(fig_test, fig_ref):
275275
@image_comparison(baseline_images=["autoscale_tiny_range"], remove_text=True)
276276
def test_autoscale_tiny_range():
277277
# github pull #904
278-
fig, ax = plt.subplots(2, 2)
279-
ax = ax.flatten()
280-
for i in range(4):
278+
fig, axs = plt.subplots(2, 2)
279+
for i, ax in enumerate(axs.flat):
281280
y1 = 10**(-11 - i)
282-
ax[i].plot([0, 1], [1, 1 + y1])
281+
ax.plot([0, 1], [1, 1 + y1])
283282

284283

285284
@pytest.mark.style('default')
@@ -358,7 +357,7 @@ def test_arrow_simple():
358357
kwargs = product(length_includes_head, shape, head_starts_at_zero)
359358

360359
fig, axs = plt.subplots(3, 4)
361-
for i, (ax, kwarg) in enumerate(zip(axs.flatten(), kwargs)):
360+
for i, (ax, kwarg) in enumerate(zip(axs.flat, kwargs)):
362361
ax.set_xlim(-2, 2)
363362
ax.set_ylim(-2, 2)
364363
# Unpack kwargs

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_constrained_layout1():
4747
def test_constrained_layout2():
4848
'Test constrained_layout for 2x2 subplots'
4949
fig, axs = plt.subplots(2, 2, constrained_layout=True)
50-
for ax in axs.flatten():
50+
for ax in axs.flat:
5151
example_plot(ax, fontsize=24)
5252

5353

@@ -56,7 +56,7 @@ def test_constrained_layout2():
5656
def test_constrained_layout3():
5757
'Test constrained_layout for colorbars with subplots'
5858
fig, axs = plt.subplots(2, 2, constrained_layout=True)
59-
for nn, ax in enumerate(axs.flatten()):
59+
for nn, ax in enumerate(axs.flat):
6060
pcm = example_pcolor(ax, fontsize=24)
6161
if nn == 3:
6262
pad = 0.08
@@ -69,7 +69,7 @@ def test_constrained_layout3():
6969
def test_constrained_layout4():
7070
'Test constrained_layout for a single colorbar with subplots'
7171
fig, axs = plt.subplots(2, 2, constrained_layout=True)
72-
for ax in axs.flatten():
72+
for ax in axs.flat:
7373
pcm = example_pcolor(ax, fontsize=24)
7474
fig.colorbar(pcm, ax=axs, pad=0.01, shrink=0.6)
7575

@@ -82,7 +82,7 @@ def test_constrained_layout5():
8282
colorbar bottom
8383
'''
8484
fig, axs = plt.subplots(2, 2, constrained_layout=True)
85-
for ax in axs.flatten():
85+
for ax in axs.flat:
8686
pcm = example_pcolor(ax, fontsize=24)
8787
fig.colorbar(pcm, ax=axs,
8888
use_gridspec=False, pad=0.01, shrink=0.6,
@@ -162,7 +162,7 @@ def test_constrained_layout9():
162162
'Test for handling suptitle and for sharex and sharey'
163163
fig, axs = plt.subplots(2, 2, constrained_layout=True,
164164
sharex=False, sharey=False)
165-
for ax in axs.flatten():
165+
for ax in axs.flat:
166166
pcm = example_pcolor(ax, fontsize=24)
167167
ax.set_xlabel('')
168168
ax.set_ylabel('')
@@ -176,7 +176,7 @@ def test_constrained_layout9():
176176
def test_constrained_layout10():
177177
'Test for handling legend outside axis'
178178
fig, axs = plt.subplots(2, 2, constrained_layout=True)
179-
for ax in axs.flatten():
179+
for ax in axs.flat:
180180
ax.plot(np.arange(12), label='This is a label')
181181
ax.legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
182182

@@ -250,7 +250,7 @@ def test_constrained_layout12():
250250
def test_constrained_layout13():
251251
'Test that padding works.'
252252
fig, axs = plt.subplots(2, 2, constrained_layout=True)
253-
for ax in axs.flatten():
253+
for ax in axs.flat:
254254
pcm = example_pcolor(ax, fontsize=12)
255255
fig.colorbar(pcm, ax=ax, shrink=0.6, aspect=20., pad=0.02)
256256
fig.set_constrained_layout_pads(w_pad=24./72., h_pad=24./72.)
@@ -261,7 +261,7 @@ def test_constrained_layout13():
261261
def test_constrained_layout14():
262262
'Test that padding works.'
263263
fig, axs = plt.subplots(2, 2, constrained_layout=True)
264-
for ax in axs.flatten():
264+
for ax in axs.flat:
265265
pcm = example_pcolor(ax, fontsize=12)
266266
fig.colorbar(pcm, ax=ax, shrink=0.6, aspect=20., pad=0.02)
267267
fig.set_constrained_layout_pads(
@@ -275,7 +275,7 @@ def test_constrained_layout15():
275275
'Test that rcparams work.'
276276
rcParams['figure.constrained_layout.use'] = True
277277
fig, axs = plt.subplots(2, 2)
278-
for ax in axs.flatten():
278+
for ax in axs.flat:
279279
example_plot(ax, fontsize=12)
280280

281281

@@ -393,7 +393,7 @@ def test_colorbar_location():
393393
"""
394394

395395
fig, axs = plt.subplots(4, 5, constrained_layout=True)
396-
for ax in axs.flatten():
396+
for ax in axs.flat:
397397
pcm = example_pcolor(ax)
398398
ax.set_xlabel('')
399399
ax.set_ylabel('')

lib/matplotlib/tests/test_contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_given_colors_levels_and_extends():
206206
colors = ['red', 'yellow', 'pink', 'blue', 'black']
207207
levels = [2, 4, 8, 10]
208208

209-
for i, ax in enumerate(axes.flatten()):
209+
for i, ax in enumerate(axes.flat):
210210
filled = i % 2 == 0.
211211
extend = ['neither', 'min', 'max', 'both'][i // 2]
212212

lib/matplotlib/tests/test_figure.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,23 @@ def test_invalid_figure_add_axes():
360360

361361

362362
def test_subplots_shareax_loglabels():
363-
fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)
364-
for ax in ax_arr.flatten():
363+
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)
364+
for ax in axs.flat:
365365
ax.plot([10, 20, 30], [10, 20, 30])
366366

367367
ax.set_yscale("log")
368368
ax.set_xscale("log")
369369

370-
for ax in ax_arr[0, :]:
370+
for ax in axs[0, :]:
371371
assert 0 == len(ax.xaxis.get_ticklabels(which='both'))
372372

373-
for ax in ax_arr[1, :]:
373+
for ax in axs[1, :]:
374374
assert 0 < len(ax.xaxis.get_ticklabels(which='both'))
375375

376-
for ax in ax_arr[:, 1]:
376+
for ax in axs[:, 1]:
377377
assert 0 == len(ax.yaxis.get_ticklabels(which='both'))
378378

379-
for ax in ax_arr[:, 0]:
379+
for ax in axs[:, 0]:
380380
assert 0 < len(ax.yaxis.get_ticklabels(which='both'))
381381

382382

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ def example_plot(ax, fontsize=12, nodec=False):
9090
# axes overlapping each other.
9191

9292
fig, axs = plt.subplots(2, 2, constrained_layout=False)
93-
for ax in axs.flatten():
93+
for ax in axs.flat:
9494
example_plot(ax)
9595

9696
###############################################################################
9797
# Specifying ``constrained_layout=True`` in the call to ``plt.subplots``
9898
# causes the layout to be properly constrained.
9999

100100
fig, axs = plt.subplots(2, 2, constrained_layout=True)
101-
for ax in axs.flatten():
101+
for ax in axs.flat:
102102
example_plot(ax)
103103

104104
###############################################################################
@@ -132,7 +132,7 @@ def example_plot(ax, fontsize=12, nodec=False):
132132
# the specified axes.
133133

134134
fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
135-
for ax in axs.flatten():
135+
for ax in axs.flat:
136136
im = ax.pcolormesh(arr, **pc_kwargs)
137137
fig.colorbar(im, ax=axs, shrink=0.6)
138138

@@ -142,7 +142,7 @@ def example_plot(ax, fontsize=12, nodec=False):
142142
# still be the same size.
143143

144144
fig, axs = plt.subplots(3, 3, figsize=(4, 4), constrained_layout=True)
145-
for ax in axs.flatten():
145+
for ax in axs.flat:
146146
im = ax.pcolormesh(arr, **pc_kwargs)
147147
fig.colorbar(im, ax=axs[1:, ][:, 1], shrink=0.8)
148148
fig.colorbar(im, ax=axs[:, -1], shrink=0.6)
@@ -179,7 +179,7 @@ def example_plot(ax, fontsize=12, nodec=False):
179179
# ``constrained_layout`` can also make room for `~.figure.Figure.suptitle`.
180180

181181
fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
182-
for ax in axs.flatten():
182+
for ax in axs.flat:
183183
im = ax.pcolormesh(arr, **pc_kwargs)
184184
fig.colorbar(im, ax=axs, shrink=0.6)
185185
fig.suptitle('Big Suptitle')
@@ -265,15 +265,15 @@ def example_plot(ax, fontsize=12, nodec=False):
265265
# `~.figure.Figure.set_constrained_layout_pads`:
266266

267267
fig, axs = plt.subplots(2, 2, constrained_layout=True)
268-
for ax in axs.flatten():
268+
for ax in axs.flat:
269269
example_plot(ax, nodec=True)
270270
ax.set_xticklabels('')
271271
ax.set_yticklabels('')
272272
fig.set_constrained_layout_pads(w_pad=4./72., h_pad=4./72.,
273273
hspace=0., wspace=0.)
274274

275275
fig, axs = plt.subplots(2, 2, constrained_layout=True)
276-
for ax in axs.flatten():
276+
for ax in axs.flat:
277277
example_plot(ax, nodec=True)
278278
ax.set_xticklabels('')
279279
ax.set_yticklabels('')
@@ -288,7 +288,7 @@ def example_plot(ax, fontsize=12, nodec=False):
288288
# the above, but the space between subplots does.
289289

290290
fig, axs = plt.subplots(2, 2, constrained_layout=True)
291-
for ax in axs.flatten():
291+
for ax in axs.flat:
292292
example_plot(ax, nodec=True)
293293
ax.set_xticklabels('')
294294
ax.set_yticklabels('')
@@ -308,7 +308,7 @@ def example_plot(ax, fontsize=12, nodec=False):
308308
# of the axis it is attached to.
309309

310310
fig, axs = plt.subplots(2, 2, constrained_layout=True)
311-
for ax in axs.flatten():
311+
for ax in axs.flat:
312312
pc = ax.pcolormesh(arr, **pc_kwargs)
313313
fig.colorbar(pc, ax=ax, shrink=0.6, pad=0)
314314
ax.set_xticklabels('')
@@ -322,7 +322,7 @@ def example_plot(ax, fontsize=12, nodec=False):
322322
# for ``pad`` to be non-zero.
323323

324324
fig, axs = plt.subplots(2, 2, constrained_layout=True)
325-
for ax in axs.flatten():
325+
for ax in axs.flat:
326326
pc = ax.pcolormesh(arr, **pc_kwargs)
327327
fig.colorbar(im, ax=ax, shrink=0.6, pad=0.05)
328328
ax.set_xticklabels('')
@@ -347,7 +347,7 @@ def example_plot(ax, fontsize=12, nodec=False):
347347

348348
plt.rcParams['figure.constrained_layout.use'] = True
349349
fig, axs = plt.subplots(2, 2, figsize=(3, 3))
350-
for ax in axs.flatten():
350+
for ax in axs.flat:
351351
example_plot(ax)
352352

353353
#############################
@@ -739,10 +739,10 @@ def docomplicated(suptitle=None):
739739
# layoutboxes in the gridspec, and it is made to be centered between
740740
# those two points.
741741

742-
fig, ax = plt.subplots(2, 2, constrained_layout=True)
743-
for a in ax.flatten():
744-
im = a.pcolormesh(arr, **pc_kwargs)
745-
fig.colorbar(im, ax=ax, shrink=0.6)
742+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
743+
for ax in axs.flat:
744+
im = ax.pcolormesh(arr, **pc_kwargs)
745+
fig.colorbar(im, ax=axs, shrink=0.6)
746746
plot_children(fig, fig._layoutbox, printit=False)
747747

748748
#######################################################################

tutorials/text/text_intro.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,23 @@
329329
# labels fit in the right-hand plot.
330330

331331
fig, axs = plt.subplots(2, 2, figsize=(8, 5), tight_layout=True)
332-
axs = axs.flatten()
333-
for n, ax in enumerate(axs):
332+
for n, ax in enumerate(axs.flat):
334333
ax.plot(x1*10., y1)
335334

336335
formatter = matplotlib.ticker.FormatStrFormatter('%1.1f')
337336
locator = matplotlib.ticker.MaxNLocator(nbins='auto', steps=[1, 4, 10])
338-
axs[1].xaxis.set_major_locator(locator)
339-
axs[1].xaxis.set_major_formatter(formatter)
337+
axs[0, 1].xaxis.set_major_locator(locator)
338+
axs[0, 1].xaxis.set_major_formatter(formatter)
340339

341340
formatter = matplotlib.ticker.FormatStrFormatter('%1.5f')
342341
locator = matplotlib.ticker.AutoLocator()
343-
axs[2].xaxis.set_major_formatter(formatter)
344-
axs[2].xaxis.set_major_locator(locator)
342+
axs[1, 0].xaxis.set_major_formatter(formatter)
343+
axs[1, 0].xaxis.set_major_locator(locator)
345344

346345
formatter = matplotlib.ticker.FormatStrFormatter('%1.5f')
347346
locator = matplotlib.ticker.MaxNLocator(nbins=4)
348-
axs[3].xaxis.set_major_formatter(formatter)
349-
axs[3].xaxis.set_major_locator(locator)
347+
axs[1, 1].xaxis.set_major_formatter(formatter)
348+
axs[1, 1].xaxis.set_major_locator(locator)
350349

351350
plt.show()
352351

0 commit comments

Comments
 (0)