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

Skip to content

Commit 21b102e

Browse files
authored
Merge pull request #11438 from jklymak/enh-get-gridspec
ENH: add get_gridspec convenience method to subplots
2 parents c351e78 + 740ec16 commit 21b102e

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Add ``ax.get_gridspec`` to `.SubplotBase`
2+
-----------------------------------------
3+
4+
New method `.SubplotBase.get_gridspec` is added so that users can
5+
easily get the gridspec that went into making an axes:
6+
7+
.. code::
8+
9+
import matplotlib.pyplot as plt
10+
11+
fig, axs = plt.subplots(3, 2)
12+
gs = axs[0, -1].get_gridspec()
13+
14+
# remove the last column
15+
for ax in axs[:,-1].flatten():
16+
ax.remove()
17+
18+
# make a subplot in last column that spans rows.
19+
ax = fig.add_subplot(gs[:, -1])
20+
plt.show()
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()

lib/matplotlib/axes/_subplots.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def set_subplotspec(self, subplotspec):
116116
"""set the SubplotSpec instance associated with the subplot"""
117117
self._subplotspec = subplotspec
118118

119+
def get_gridspec(self):
120+
"""get the GridSpec instance associated with the subplot"""
121+
return self._subplotspec.get_gridspec()
122+
119123
def update_params(self):
120124
"""update the subplot position from fig.subplotpars"""
121125

lib/matplotlib/tests/test_subplots.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,9 @@ def test_subplots_offsettext():
143143
axes[1, 0].plot(x, x)
144144
axes[0, 1].plot(y, x)
145145
axes[1, 1].plot(y, x)
146+
147+
148+
def test_get_gridspec():
149+
# ahem, pretty trivial, but...
150+
fig, ax = plt.subplots()
151+
assert ax.get_subplotspec().get_gridspec() == ax.get_gridspec()

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)