Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use GridSpec in plt.subplots #2647

Merged
merged 1 commit into from Jan 13, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/matplotlib/pyplot.py
Expand Up @@ -917,7 +917,7 @@ def subplot(*args, **kwargs):


def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, **fig_kw):
subplot_kw=None, gridspec_kw=None, **fig_kw):
"""
Create a figure with a set of subplots already made.

Expand Down Expand Up @@ -977,6 +977,11 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
:meth:`~matplotlib.figure.Figure.add_subplot` call used to
create each subplots.

*gridspec_kw* : dict
Dict with keywords passed to the
:class:`~matplotlib.gridspec.GridSpec` constructor used to create
the grid the subplots are placed on.

*fig_kw* : dict
Dict with keywords passed to the :func:`figure` call. Note that all
keywords not recognized above will be automatically included here.
Expand Down Expand Up @@ -1051,16 +1056,19 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
(sharey, share_values))
if subplot_kw is None:
subplot_kw = {}
if gridspec_kw is None:
gridspec_kw = {}

fig = figure(**fig_kw)
gs = GridSpec(nrows, ncols, **gridspec_kw)

# Create empty object array to hold all axes. It's easiest to make it 1-d
# so we can just append subplots upon creation, and then
nplots = nrows*ncols
axarr = np.empty(nplots, dtype=object)

# Create first subplot separately, so we can share it if requested
ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)
ax0 = fig.add_subplot(gs[0, 0], **subplot_kw)
#if sharex:
# subplot_kw['sharex'] = ax0
#if sharey:
Expand Down Expand Up @@ -1090,7 +1098,7 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw['sharey'] = None
else:
subplot_kw['sharey'] = axarr[sys[i]]
axarr[i] = fig.add_subplot(nrows, ncols, i + 1, **subplot_kw)
axarr[i] = fig.add_subplot(gs[i // ncols, i % ncols], **subplot_kw)

# returned axis array will be always 2-d, even if nrows=ncols=1
axarr = axarr.reshape(nrows, ncols)
Expand Down