Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1757e7b

Browse filesBrowse files
jklymakMeeseeksDev[bot]
authored andcommitted
Backport PR #14079: Consistently use axs.flat instead of axs.flatten()
1 parent b9d3c83 commit 1757e7b
Copy full SHA for 1757e7b

File tree

Expand file treeCollapse file tree

11 files changed

+51
-53
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+51
-53
lines changed

‎examples/specialty_plots/radar_chart.py

Copy file name to clipboardExpand all lines: examples/specialty_plots/radar_chart.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: examples/statistics/boxplot.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: examples/statistics/bxp.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: examples/statistics/violinplot.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: examples/subplots_axes_and_figures/demo_constrained_layout.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,10 @@ def test_minorticks_on_rcParams_both(fig_test, fig_ref):
280280
@image_comparison(baseline_images=["autoscale_tiny_range"], remove_text=True)
281281
def test_autoscale_tiny_range():
282282
# github pull #904
283-
fig, ax = plt.subplots(2, 2)
284-
ax = ax.flatten()
285-
for i in range(4):
283+
fig, axs = plt.subplots(2, 2)
284+
for i, ax in enumerate(axs.flat):
286285
y1 = 10**(-11 - i)
287-
ax[i].plot([0, 1], [1, 1 + y1])
286+
ax.plot([0, 1], [1, 1 + y1])
288287

289288

290289
@pytest.mark.style('default')
@@ -363,7 +362,7 @@ def test_arrow_simple():
363362
kwargs = product(length_includes_head, shape, head_starts_at_zero)
364363

365364
fig, axs = plt.subplots(3, 4)
366-
for i, (ax, kwarg) in enumerate(zip(axs.flatten(), kwargs)):
365+
for i, (ax, kwarg) in enumerate(zip(axs.flat, kwargs)):
367366
ax.set_xlim(-2, 2)
368367
ax.set_ylim(-2, 2)
369368
# Unpack kwargs

‎lib/matplotlib/tests/test_constrainedlayout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_constrainedlayout.py
+10-10Lines 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,
@@ -163,7 +163,7 @@ def test_constrained_layout9():
163163
'Test for handling suptitle and for sharex and sharey'
164164
fig, axs = plt.subplots(2, 2, constrained_layout=True,
165165
sharex=False, sharey=False)
166-
for ax in axs.flatten():
166+
for ax in axs.flat:
167167
pcm = example_pcolor(ax, fontsize=24)
168168
ax.set_xlabel('')
169169
ax.set_ylabel('')
@@ -177,7 +177,7 @@ def test_constrained_layout9():
177177
def test_constrained_layout10():
178178
'Test for handling legend outside axis'
179179
fig, axs = plt.subplots(2, 2, constrained_layout=True)
180-
for ax in axs.flatten():
180+
for ax in axs.flat:
181181
ax.plot(np.arange(12), label='This is a label')
182182
ax.legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
183183

@@ -251,7 +251,7 @@ def test_constrained_layout12():
251251
def test_constrained_layout13():
252252
'Test that padding works.'
253253
fig, axs = plt.subplots(2, 2, constrained_layout=True)
254-
for ax in axs.flatten():
254+
for ax in axs.flat:
255255
pcm = example_pcolor(ax, fontsize=12)
256256
fig.colorbar(pcm, ax=ax, shrink=0.6, aspect=20., pad=0.02)
257257
fig.set_constrained_layout_pads(w_pad=24./72., h_pad=24./72.)
@@ -262,7 +262,7 @@ def test_constrained_layout13():
262262
def test_constrained_layout14():
263263
'Test that padding works.'
264264
fig, axs = plt.subplots(2, 2, constrained_layout=True)
265-
for ax in axs.flatten():
265+
for ax in axs.flat:
266266
pcm = example_pcolor(ax, fontsize=12)
267267
fig.colorbar(pcm, ax=ax, shrink=0.6, aspect=20., pad=0.02)
268268
fig.set_constrained_layout_pads(
@@ -276,7 +276,7 @@ def test_constrained_layout15():
276276
'Test that rcparams work.'
277277
rcParams['figure.constrained_layout.use'] = True
278278
fig, axs = plt.subplots(2, 2)
279-
for ax in axs.flatten():
279+
for ax in axs.flat:
280280
example_plot(ax, fontsize=12)
281281

282282

@@ -394,7 +394,7 @@ def test_colorbar_location():
394394
"""
395395

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

‎lib/matplotlib/tests/test_contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_contour.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,23 +343,23 @@ def test_invalid_figure_size():
343343

344344

345345
def test_subplots_shareax_loglabels():
346-
fig, ax_arr = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)
347-
for ax in ax_arr.flatten():
346+
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)
347+
for ax in axs.flat:
348348
ax.plot([10, 20, 30], [10, 20, 30])
349349

350350
ax.set_yscale("log")
351351
ax.set_xscale("log")
352352

353-
for ax in ax_arr[0, :]:
353+
for ax in axs[0, :]:
354354
assert 0 == len(ax.xaxis.get_ticklabels(which='both'))
355355

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

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

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

365365

‎tutorials/intermediate/constrainedlayout_guide.py

Copy file name to clipboardExpand all lines: tutorials/intermediate/constrainedlayout_guide.py
+15-15Lines 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

Copy file name to clipboardExpand all lines: tutorials/text/text_intro.py
+7-8Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.