Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c42d198

Browse files
authored
Merge pull request #17057 from anntzer/subgrid
Simplify subgridspec example/tutorial.
2 parents 9f9c2bb + 431e993 commit c42d198

File tree

2 files changed

+32
-57
lines changed

2 files changed

+32
-57
lines changed

examples/userdemo/demo_gridspec06.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
"""
88

99
import matplotlib.pyplot as plt
10-
import matplotlib.gridspec as gridspec
1110
import numpy as np
12-
from itertools import product
1311

1412

1513
def squiggle_xy(a, b, c, d):
@@ -18,35 +16,23 @@ def squiggle_xy(a, b, c, d):
1816

1917

2018
fig = plt.figure(figsize=(8, 8))
21-
22-
# gridspec inside gridspec
23-
outer_grid = gridspec.GridSpec(4, 4, wspace=0.0, hspace=0.0)
24-
25-
for i in range(16):
26-
inner_grid = gridspec.GridSpecFromSubplotSpec(
27-
3, 3, subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0)
28-
a = i // 4 + 1
29-
b = i % 4 + 1
30-
for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
31-
ax = fig.add_subplot(inner_grid[j])
32-
ax.plot(*squiggle_xy(a, b, c, d))
33-
ax.set_xticks([])
34-
ax.set_yticks([])
35-
fig.add_subplot(ax)
36-
37-
all_axes = fig.get_axes()
19+
outer_grid = fig.add_gridspec(4, 4, wspace=0, hspace=0)
20+
21+
for a in range(4):
22+
for b in range(4):
23+
# gridspec inside gridspec
24+
inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0)
25+
for c in range(3):
26+
for d in range(3):
27+
ax = fig.add_subplot(inner_grid[c, d])
28+
ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1))
29+
ax.set(xticks=[], yticks=[])
3830

3931
# show only the outside spines
40-
for ax in all_axes:
41-
for sp in ax.spines.values():
42-
sp.set_visible(False)
43-
if ax.is_first_row():
44-
ax.spines['top'].set_visible(True)
45-
if ax.is_last_row():
46-
ax.spines['bottom'].set_visible(True)
47-
if ax.is_first_col():
48-
ax.spines['left'].set_visible(True)
49-
if ax.is_last_col():
50-
ax.spines['right'].set_visible(True)
32+
for ax in fig.get_axes():
33+
ax.spines['top'].set_visible(ax.is_first_row())
34+
ax.spines['bottom'].set_visible(ax.is_last_row())
35+
ax.spines['left'].set_visible(ax.is_first_col())
36+
ax.spines['right'].set_visible(ax.is_last_col())
5137

5238
plt.show()

tutorials/intermediate/gridspec.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -226,42 +226,31 @@
226226
# spines in each of the inner 3x3 grids.
227227

228228
import numpy as np
229-
from itertools import product
230229

231230

232231
def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
233232
return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)
234233

235234

236235
fig11 = plt.figure(figsize=(8, 8), constrained_layout=False)
237-
238-
# gridspec inside gridspec
239-
outer_grid = fig11.add_gridspec(4, 4, wspace=0.0, hspace=0.0)
240-
241-
for i in range(16):
242-
inner_grid = outer_grid[i].subgridspec(3, 3, wspace=0.0, hspace=0.0)
243-
a, b = int(i/4)+1, i % 4+1
244-
for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
245-
ax = fig11.add_subplot(inner_grid[j])
246-
ax.plot(*squiggle_xy(a, b, c, d))
247-
ax.set_xticks([])
248-
ax.set_yticks([])
249-
fig11.add_subplot(ax)
250-
251-
all_axes = fig11.get_axes()
236+
outer_grid = fig11.add_gridspec(4, 4, wspace=0, hspace=0)
237+
238+
for a in range(4):
239+
for b in range(4):
240+
# gridspec inside gridspec
241+
inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0)
242+
for c in range(3):
243+
for d in range(3):
244+
ax = fig11.add_subplot(inner_grid[c, d])
245+
ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1))
246+
ax.set(xticks=[], yticks=[])
252247

253248
# show only the outside spines
254-
for ax in all_axes:
255-
for sp in ax.spines.values():
256-
sp.set_visible(False)
257-
if ax.is_first_row():
258-
ax.spines['top'].set_visible(True)
259-
if ax.is_last_row():
260-
ax.spines['bottom'].set_visible(True)
261-
if ax.is_first_col():
262-
ax.spines['left'].set_visible(True)
263-
if ax.is_last_col():
264-
ax.spines['right'].set_visible(True)
249+
for ax in fig11.get_axes():
250+
ax.spines['top'].set_visible(ax.is_first_row())
251+
ax.spines['bottom'].set_visible(ax.is_last_row())
252+
ax.spines['left'].set_visible(ax.is_first_col())
253+
ax.spines['right'].set_visible(ax.is_last_col())
265254

266255
plt.show()
267256

0 commit comments

Comments
 (0)