.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_userdemo_demo_gridspec06.py: ================ Nested GridSpecs ================ This example demonstrates the use of nested `GridSpec`\s. .. image:: /gallery/userdemo/images/sphx_glr_demo_gridspec06_001.png :class: sphx-glr-single-img .. code-block:: default 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): 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) 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() # 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) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.525 seconds) .. _sphx_glr_download_gallery_userdemo_demo_gridspec06.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: demo_gridspec06.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: demo_gridspec06.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature Keywords: matplotlib code example, codex, python plot, pyplot `Gallery generated by Sphinx-Gallery `_