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

Skip to content

Commit 101b34e

Browse files
committed
DOC: add tutorial and example
1 parent 1bf15da commit 101b34e

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

doc/users/next_whats_new/subplot_get_gridspec.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Add ``ax.get_gridspec`` to `.SubplotBase`
22
-----------------------------------------
33

44
New method `.SubplotBase.get_gridspec` is added so that users can
5-
easily get the gridspec that went into making as axes:
5+
easily get the gridspec that went into making an axes:
66

77
.. code::
88
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
==================================================
3+
Combining two subplots using Subplots and gridspec
4+
==================================================
5+
6+
Sometimes we want to combine two subplots in an axes layout created with
7+
`.figure.subplots`. We can get the `.gridspec.Gridspec` from the axes
8+
and then remove the covered axes and fill the gap with a new bigger axes.
9+
Here we create a layout with the bottom two axes in the last column combined.
10+
11+
See: :doc:`/tutorials/intermediate/gridspec`
12+
"""
13+
import matplotlib.pyplot as plt
14+
15+
fig, axs = plt.subplots(ncols=3, nrows=3)
16+
gs = axs[1, 2].get_gridspec()
17+
# remove the underlying axes
18+
for ax in axs[1:, -1]:
19+
ax.remove()
20+
axbig = fig.add_subplot(gs[1:, -1])
21+
axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
22+
xycoords='axes fraction', va='center')
23+
24+
fig.tight_layout()

tutorials/intermediate/gridspec.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@
122122

123123
fig5.tight_layout()
124124

125+
############################################################################
126+
# The ``subplots`` and ``gridspec`` methods can be combined since it is
127+
# sometimes more convenient to make most of the subplots using ``subplots``
128+
# and then remove some and combine them. Here we create a layout with
129+
# the bottom two axes in the last column combined.
130+
131+
fig, axs = plt.subplots(ncols=3, nrows=3)
132+
gs = axs[1, 2].get_gridspec()
133+
# remove the underlying axes
134+
for ax in axs[1:, -1]:
135+
ax.remove()
136+
axbig = fig.add_subplot(gs[1:, -1])
137+
axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
138+
xycoords='axes fraction', va='center')
139+
140+
fig.tight_layout()
125141

126142
###############################################################################
127143
# Fine Adjustments to a Gridspec Layout

0 commit comments

Comments
 (0)