From 18d20e556ef94105869fe874ea52607c34c08b8d Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 14 Jun 2018 13:29:27 -0700 Subject: [PATCH 1/3] ENH: add get_gridspec convenience method to subplots --- .../next_whats_new/subplot_get_gridspec.rst | 20 +++++++++++++++++++ lib/matplotlib/axes/_subplots.py | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 doc/users/next_whats_new/subplot_get_gridspec.rst diff --git a/doc/users/next_whats_new/subplot_get_gridspec.rst b/doc/users/next_whats_new/subplot_get_gridspec.rst new file mode 100644 index 000000000000..9d6cfde3e387 --- /dev/null +++ b/doc/users/next_whats_new/subplot_get_gridspec.rst @@ -0,0 +1,20 @@ +Add ``ax.get_gridspec`` to `.SubplotBase` +----------------------------------------- + +New method `.SubplotBase.get_gridspec` is added so that users can +easily get the gridspec that went into making as axes: + + .. code:: + + import matplotlib.pyplot as plt + + fig, axs = plt.subplots(3, 2) + gs = axs[0, -1].get_gridspec() + + # remove the last column + for ax in axs[:,-1].flatten(): + ax.remove() + + # make a subplot in last column that spans rows. + ax = fig.add_subplot(gs[:, -1]) + plt.show() diff --git a/lib/matplotlib/axes/_subplots.py b/lib/matplotlib/axes/_subplots.py index 257c5711f2de..b42cf4f5b9f9 100644 --- a/lib/matplotlib/axes/_subplots.py +++ b/lib/matplotlib/axes/_subplots.py @@ -117,6 +117,10 @@ def set_subplotspec(self, subplotspec): """set the SubplotSpec instance associated with the subplot""" self._subplotspec = subplotspec + def get_gridspec(self): + """get the GridSpec instance associated with the subplot""" + return self._subplotspec.get_gridspec() + def update_params(self): """update the subplot position from fig.subplotpars""" From a8121089a4f494322796e6c36740ffb9ba676147 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 14 Jun 2018 13:38:16 -0700 Subject: [PATCH 2/3] TST: Add trivial test --- lib/matplotlib/tests/test_subplots.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index 9a1a5e7f7a8d..30cebe656e58 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -143,3 +143,9 @@ def test_subplots_offsettext(): axes[1, 0].plot(x, x) axes[0, 1].plot(y, x) axes[1, 1].plot(y, x) + + +def test_get_gridspec(): + # ahem, pretty trivial, but... + fig, ax = plt.subplots() + assert ax.get_subplotspec().get_gridspec() == ax.get_gridspec() From 740ec166081dbb4d3cf9b971eb167fe45e706302 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 4 Jul 2018 12:43:23 -0700 Subject: [PATCH 3/3] DOC: add tutorial and example --- .../next_whats_new/subplot_get_gridspec.rst | 2 +- .../gridspec_and_subplots.py | 24 +++++++++++++++++++ tutorials/intermediate/gridspec.py | 16 +++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 examples/subplots_axes_and_figures/gridspec_and_subplots.py diff --git a/doc/users/next_whats_new/subplot_get_gridspec.rst b/doc/users/next_whats_new/subplot_get_gridspec.rst index 9d6cfde3e387..2f6841303b42 100644 --- a/doc/users/next_whats_new/subplot_get_gridspec.rst +++ b/doc/users/next_whats_new/subplot_get_gridspec.rst @@ -2,7 +2,7 @@ Add ``ax.get_gridspec`` to `.SubplotBase` ----------------------------------------- New method `.SubplotBase.get_gridspec` is added so that users can -easily get the gridspec that went into making as axes: +easily get the gridspec that went into making an axes: .. code:: diff --git a/examples/subplots_axes_and_figures/gridspec_and_subplots.py b/examples/subplots_axes_and_figures/gridspec_and_subplots.py new file mode 100644 index 000000000000..a263ef8893a2 --- /dev/null +++ b/examples/subplots_axes_and_figures/gridspec_and_subplots.py @@ -0,0 +1,24 @@ +""" +================================================== +Combining two subplots using subplots and GridSpec +================================================== + +Sometimes we want to combine two subplots in an axes layout created with +`~.Figure.subplots`. We can get the `~.gridspec.GridSpec` from the axes +and then remove the covered axes and fill the gap with a new bigger axes. +Here we create a layout with the bottom two axes in the last column combined. + +See: :doc:`/tutorials/intermediate/gridspec` +""" +import matplotlib.pyplot as plt + +fig, axs = plt.subplots(ncols=3, nrows=3) +gs = axs[1, 2].get_gridspec() +# remove the underlying axes +for ax in axs[1:, -1]: + ax.remove() +axbig = fig.add_subplot(gs[1:, -1]) +axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5), + xycoords='axes fraction', va='center') + +fig.tight_layout() diff --git a/tutorials/intermediate/gridspec.py b/tutorials/intermediate/gridspec.py index 54e874bcd499..64f444e4c73a 100644 --- a/tutorials/intermediate/gridspec.py +++ b/tutorials/intermediate/gridspec.py @@ -122,6 +122,22 @@ fig5.tight_layout() +############################################################################ +# The ``subplots`` and ``gridspec`` methods can be combined since it is +# sometimes more convenient to make most of the subplots using ``subplots`` +# and then remove some and combine them. Here we create a layout with +# the bottom two axes in the last column combined. + +fig, axs = plt.subplots(ncols=3, nrows=3) +gs = axs[1, 2].get_gridspec() +# remove the underlying axes +for ax in axs[1:, -1]: + ax.remove() +axbig = fig.add_subplot(gs[1:, -1]) +axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5), + xycoords='axes fraction', va='center') + +fig.tight_layout() ############################################################################### # Fine Adjustments to a Gridspec Layout