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

Skip to content

Commit 770cdaf

Browse files
committed
Allow sharing axis with axes from other figure.
1 parent 2188187 commit 770cdaf

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lib/matplotlib/pyplot.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,21 +820,25 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
820820
*ncols* : int
821821
Number of columns of the subplot grid. Defaults to 1.
822822
823-
*sharex* : string or bool
823+
*sharex* : axes instance, string or bool
824824
If *True*, the X axis will be shared amongst all subplots. If
825825
*True* and you have multiple rows, the x tick labels on all but
826-
the last row of plots will have visible set to *False*
826+
the last row of plots will have visible set to *False*.
827+
If an axes instance, same as *True* and the X axis is also shared
828+
with the X axis of the provided axes instance.
827829
If a string must be one of "row", "col", "all", or "none".
828830
"all" has the same effect as *True*, "none" has the same effect
829831
as *False*.
830832
If "row", each subplot row will share a X axis.
831833
If "col", each subplot column will share a X axis and the x tick
832834
labels on all but the last row will have visible set to *False*.
833835
834-
*sharey* : string or bool
836+
*sharey* : axes instance, string or bool
835837
If *True*, the Y axis will be shared amongst all subplots. If
836838
*True* and you have multiple columns, the y tick labels on all but
837-
the first column of plots will have visible set to *False*
839+
the first column of plots will have visible set to *False*.
840+
If an axes instance, same as *True* and the Y axis is also shared
841+
with the Y axis of the provided axes instance.
838842
If a string must be one of "row", "col", "all", or "none".
839843
"all" has the same effect as *True*, "none" has the same effect
840844
as *False*.
@@ -910,6 +914,8 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
910914
# same as
911915
plt.subplots(2, 2, sharex=True, sharey=True)
912916
"""
917+
if subplot_kw is None:
918+
subplot_kw = {}
913919
# for backwards compatibility
914920
if isinstance(sharex, bool):
915921
if sharex:
@@ -921,6 +927,13 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
921927
sharey = "all"
922928
else:
923929
sharey = "none"
930+
if isinstance(sharex, Axes):
931+
subplot_kw['sharex'] = sharex
932+
sharex = "all"
933+
if isinstance(sharex, Axes):
934+
subplot_kw['sharey'] = sharey
935+
sharey = "all"
936+
924937
share_values = ["all", "row", "col", "none"]
925938
if sharex not in share_values:
926939
# This check was added because it is very easy to type subplots(1, 2, 1)
@@ -937,8 +950,6 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
937950
if sharey not in share_values:
938951
raise ValueError("sharey [%s] must be one of %s" % \
939952
(sharey, share_values))
940-
if subplot_kw is None:
941-
subplot_kw = {}
942953

943954
fig = figure(**fig_kw)
944955

0 commit comments

Comments
 (0)