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

Skip to content

Commit 18d20e5

Browse files
committed
ENH: add get_gridspec convenience method to subplots
1 parent 9d81809 commit 18d20e5

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

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 as 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()

lib/matplotlib/axes/_subplots.py

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

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

0 commit comments

Comments
 (0)