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

Skip to content

Simplify subgridspec example/tutorial. #17057

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
Apr 7, 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
46 changes: 16 additions & 30 deletions examples/userdemo/demo_gridspec06.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"""

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
from itertools import product


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


fig = plt.figure(figsize=(8, 8))

# gridspec inside gridspec
outer_grid = gridspec.GridSpec(4, 4, wspace=0.0, hspace=0.0)

for i in range(16):
inner_grid = gridspec.GridSpecFromSubplotSpec(
3, 3, subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0)
a = i // 4 + 1
b = i % 4 + 1
for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
ax = fig.add_subplot(inner_grid[j])
ax.plot(*squiggle_xy(a, b, c, d))
ax.set_xticks([])
ax.set_yticks([])
fig.add_subplot(ax)

all_axes = fig.get_axes()
outer_grid = fig.add_gridspec(4, 4, wspace=0, hspace=0)

for a in range(4):
for b in range(4):
# gridspec inside gridspec
inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0)
for c in range(3):
for d in range(3):
ax = fig.add_subplot(inner_grid[c, d])
ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1))
ax.set(xticks=[], yticks=[])

# show only the outside spines
for ax in all_axes:
for sp in ax.spines.values():
sp.set_visible(False)
if ax.is_first_row():
ax.spines['top'].set_visible(True)
if ax.is_last_row():
ax.spines['bottom'].set_visible(True)
if ax.is_first_col():
ax.spines['left'].set_visible(True)
if ax.is_last_col():
ax.spines['right'].set_visible(True)
for ax in fig.get_axes():
ax.spines['top'].set_visible(ax.is_first_row())
ax.spines['bottom'].set_visible(ax.is_last_row())
ax.spines['left'].set_visible(ax.is_first_col())
ax.spines['right'].set_visible(ax.is_last_col())

plt.show()
43 changes: 16 additions & 27 deletions tutorials/intermediate/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,42 +226,31 @@
# spines in each of the inner 3x3 grids.

import numpy as np
from itertools import product


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


fig11 = plt.figure(figsize=(8, 8), constrained_layout=False)

# gridspec inside gridspec
outer_grid = fig11.add_gridspec(4, 4, wspace=0.0, hspace=0.0)

for i in range(16):
inner_grid = outer_grid[i].subgridspec(3, 3, wspace=0.0, hspace=0.0)
a, b = int(i/4)+1, i % 4+1
for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
ax = fig11.add_subplot(inner_grid[j])
ax.plot(*squiggle_xy(a, b, c, d))
ax.set_xticks([])
ax.set_yticks([])
fig11.add_subplot(ax)

all_axes = fig11.get_axes()
outer_grid = fig11.add_gridspec(4, 4, wspace=0, hspace=0)

for a in range(4):
for b in range(4):
# gridspec inside gridspec
inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0)
for c in range(3):
for d in range(3):
ax = fig11.add_subplot(inner_grid[c, d])
ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1))
ax.set(xticks=[], yticks=[])

# show only the outside spines
for ax in all_axes:
for sp in ax.spines.values():
sp.set_visible(False)
if ax.is_first_row():
ax.spines['top'].set_visible(True)
if ax.is_last_row():
ax.spines['bottom'].set_visible(True)
if ax.is_first_col():
ax.spines['left'].set_visible(True)
if ax.is_last_col():
ax.spines['right'].set_visible(True)
for ax in fig11.get_axes():
ax.spines['top'].set_visible(ax.is_first_row())
ax.spines['bottom'].set_visible(ax.is_last_row())
ax.spines['left'].set_visible(ax.is_first_col())
ax.spines['right'].set_visible(ax.is_last_col())

plt.show()

Expand Down