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

Skip to content

Explicitely passing an axes to share axis with #1732

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
23 changes: 17 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,21 +820,25 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
*ncols* : int
Number of columns of the subplot grid. Defaults to 1.

*sharex* : string or bool
*sharex* : axes instance, string or bool
If *True*, the X axis will be shared amongst all subplots. If
*True* and you have multiple rows, the x tick labels on all but
the last row of plots will have visible set to *False*
the last row of plots will have visible set to *False*.
If an axes instance, same as *True* and the X axis is also shared
with the X axis of the provided axes instance.
If a string must be one of "row", "col", "all", or "none".
"all" has the same effect as *True*, "none" has the same effect
as *False*.
If "row", each subplot row will share a X axis.
If "col", each subplot column will share a X axis and the x tick
labels on all but the last row will have visible set to *False*.

*sharey* : string or bool
*sharey* : axes instance, string or bool
If *True*, the Y axis will be shared amongst all subplots. If
*True* and you have multiple columns, the y tick labels on all but
the first column of plots will have visible set to *False*
the first column of plots will have visible set to *False*.
If an axes instance, same as *True* and the Y axis is also shared
with the Y axis of the provided axes instance.
If a string must be one of "row", "col", "all", or "none".
"all" has the same effect as *True*, "none" has the same effect
as *False*.
Expand Down Expand Up @@ -910,6 +914,8 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
# same as
plt.subplots(2, 2, sharex=True, sharey=True)
"""
if subplot_kw is None:
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this is the most desirable approach. Is is not possible to fix this at the source of the problem (i.e. the change that occurred between 1.1 and 1.2?

Copy link
Author

Choose a reason for hiding this comment

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

What is puzzling you exactly ? As mentioned in #1731 the commit introduce a new feature that was as a matter of fact not handled in 1.1. It avoids the redondant use of sharex as a keyword argument and in subplot_kw.

subplot_kw = {}
# for backwards compatibility
if isinstance(sharex, bool):
if sharex:
Expand All @@ -921,6 +927,13 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
sharey = "all"
else:
sharey = "none"
if isinstance(sharex, Axes):
subplot_kw['sharex'] = sharex
sharex = "all"
if isinstance(sharey, Axes):
subplot_kw['sharey'] = sharey
sharey = "all"

share_values = ["all", "row", "col", "none"]
if sharex not in share_values:
# This check was added because it is very easy to type subplots(1, 2, 1)
Expand All @@ -937,8 +950,6 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
if sharey not in share_values:
raise ValueError("sharey [%s] must be one of %s" % \
(sharey, share_values))
if subplot_kw is None:
subplot_kw = {}

fig = figure(**fig_kw)

Expand Down