Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Move impl. of plt.subplots to Figure.add_subplots. #5146

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

Merged
merged 3 commits into from
Nov 22, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Rename to Figure.subplots; typo fixes.
  • Loading branch information
anntzer committed Oct 12, 2015
commit eded0757757538d730dcfe0550e7dd60b1c89479
32 changes: 14 additions & 18 deletions 32 lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,8 @@ def add_subplot(self, *args, **kwargs):
self.stale = True
return a

def add_subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
squeeze=True, subplot_kw=None, gridspec_kw=None):
def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
squeeze=True, subplot_kw=None, gridspec_kw=None):
"""
Add a set of subplots to this figure.

Expand All @@ -1029,10 +1029,10 @@ def add_subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
sharey : {"none", "all", "row", "col"} or bool, default: False
If *False*, or "none", each subplot has its own Y axis.

If *True*, or "all", all subplots will share an Y axis, and the x
If *True*, or "all", all subplots will share an Y axis, and the y
tick labels on all but the first column of plots will be invisible.

If "row", each subplot row will share an Y axis, and the x tick
If "row", each subplot row will share an Y axis, and the y tick
labels on all but the first column of plots will be invisible.

If "col", each subplot column will share an Y axis.
Expand All @@ -1042,16 +1042,15 @@ def add_subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
array:

- if only one subplot is constructed (nrows=ncols=1), the resulting
single Axes object is returned as a scalar.
single Axes object is returned as a scalar.

- for Nx1 or 1xN subplots, the returned object is a 1-d numpy
object array of Axes objects are returned as numpy 1-d arrays.
object array of Axes objects are returned as numpy 1-d arrays.

- for NxM subplots with N>1 and M>1 are returned as a 2d array.

If *False*, no squeezing at all is done: the returned axes object
is always a 2-d array of Axes instances, even if it ends up being
1x1.
If *False*, no squeezing at all is done: the returned object is
always a 2-d array of Axes instances, even if it ends up being 1x1.

subplot_kw : dict, default: {}
Dict with keywords passed to the
Expand All @@ -1066,7 +1065,7 @@ def add_subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
Returns
-------
ax : single Axes object or array of Axes objects
The addes axes. The dimensions of the resulting array can be
The added axes. The dimensions of the resulting array can be
controlled with the squeeze keyword, see above.

See Also
Expand Down Expand Up @@ -1114,30 +1113,27 @@ def add_subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
axarr[row, col] = self.add_subplot(gs[row, col], **subplot_kw)

# turn off redundant tick labeling
if sharex in ["col", "all"] and nrows > 1:
if sharex in ["col", "all"]:
# turn off all but the bottom row
for ax in axarr[:-1, :].flat:
for label in ax.get_xticklabels():
label.set_visible(False)
ax.xaxis.offsetText.set_visible(False)

if sharey in ["row", "all"] and ncols > 1:
if sharey in ["row", "all"]:
# turn off all but the first column
for ax in axarr[:, 1:].flat:
for label in ax.get_yticklabels():
label.set_visible(False)
ax.yaxis.offsetText.set_visible(False)

if squeeze:
# Reshape the array to have the final desired dimension (nrow,ncol),
# though discarding unneeded dimensions that equal 1. If we only have
# one subplot, just return it instead of a 1-element array.
# Discarding unneeded dimensions that equal 1. If we only have one
# subplot, just return it instead of a 1-element array.
return axarr.item() if axarr.size == 1 else axarr.squeeze()
else:
# returned axis array will be always 2-d, even if nrows=ncols=1
# Returned axis array will be always 2-d, even if nrows=ncols=1.
return axarr


def clf(self, keep_observers=False):
"""
Clear the figure.
Expand Down
6 changes: 3 additions & 3 deletions 6 lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,9 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
plt.subplots(2, 2, sharex=True, sharey=True)
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we eliminate a lot of this docstring? Either with @_autogen_docstring or %(Figure.subplots)s or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to make this work, given that the APIs are subtly different (one returns just the new axes, the other returns a figure, axes pair). Open to suggestions...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super worried about the duplication of docstrings at this point. An interesting follow on to this PR would be to generate plt.subplots() via boilerplate.py.

fig = figure(**fig_kw)
axs = fig.add_subplots(
nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey, squeeze=squeeze,
subplot_kw=subplot_kw, gridspec_kw=gridspec_kw)
axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
squeeze=squeeze, subplot_kw=subplot_kw,
gridspec_kw=gridspec_kw)
return fig, axs


Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.