From 431e99306de7d5b988d6a750446fd57ee08b6323 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 7 Apr 2020 16:11:16 +0200 Subject: [PATCH] Simplify subgridspec example/tutorial. - Use add_gridspec to construct the gridspec and subgridspec to construct the subgridspec, saving an import. - Use nested loops rather than itertools.product, which is nice but not really the point of the tutorial. - Index gridspecs with two indices rather than a single flat index, again matching "standard" use. - Set spine visiibility to the right value directly, rather than first toggling them all off and then a few on again. --- examples/userdemo/demo_gridspec06.py | 46 ++++++++++------------------ tutorials/intermediate/gridspec.py | 43 ++++++++++---------------- 2 files changed, 32 insertions(+), 57 deletions(-) diff --git a/examples/userdemo/demo_gridspec06.py b/examples/userdemo/demo_gridspec06.py index 7bf7886ab0bd..507c497b4486 100644 --- a/examples/userdemo/demo_gridspec06.py +++ b/examples/userdemo/demo_gridspec06.py @@ -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): @@ -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() diff --git a/tutorials/intermediate/gridspec.py b/tutorials/intermediate/gridspec.py index e1b007a10020..8da66d3eed7a 100644 --- a/tutorials/intermediate/gridspec.py +++ b/tutorials/intermediate/gridspec.py @@ -226,7 +226,6 @@ # 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)): @@ -234,34 +233,24 @@ def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)): 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() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy