@@ -432,16 +432,8 @@ def __init__(self, fig, rect,
432
432
self ._anchor = 'C'
433
433
self ._stale_viewlim_x = False
434
434
self ._stale_viewlim_y = False
435
- self ._sharex = sharex
436
- self ._sharey = sharey
437
- if sharex is not None :
438
- if not isinstance (sharex , _AxesBase ):
439
- raise TypeError ('sharex must be an axes, not a bool' )
440
- self ._shared_x_axes .join (self , sharex )
441
- if sharey is not None :
442
- if not isinstance (sharey , _AxesBase ):
443
- raise TypeError ('sharey must be an axes, not a bool' )
444
- self ._shared_y_axes .join (self , sharey )
435
+ self ._sharex = None
436
+ self ._sharey = None
445
437
self .set_label (label )
446
438
self .set_figure (fig )
447
439
self .set_box_aspect (box_aspect )
@@ -460,6 +452,11 @@ def __init__(self, fig, rect,
460
452
self ._rasterization_zorder = None
461
453
self .cla ()
462
454
455
+ if sharex is not None :
456
+ self .sharex (sharex )
457
+ if sharey is not None :
458
+ self .sharey (sharey )
459
+
463
460
# funcs used to format x and y - fall back on major formatters
464
461
self .fmt_xdata = None
465
462
self .fmt_ydata = None
@@ -952,6 +949,44 @@ def _gen_axes_spines(self, locations=None, offset=0.0, units='inches'):
952
949
return OrderedDict ((side , mspines .Spine .linear_spine (self , side ))
953
950
for side in ['left' , 'right' , 'bottom' , 'top' ])
954
951
952
+ def sharex (self , other ):
953
+ """
954
+ Share the x-axis with *other*.
955
+
956
+ This is equivalent to passing ``sharex=other`` when constructing the
957
+ axes, and cannot be used if the x-axis is already being shared with
958
+ another axes.
959
+ """
960
+ cbook ._check_isinstance (_AxesBase , other = other )
961
+ if self ._sharex is not None and other is not self ._sharex :
962
+ raise ValueError ("x-axis is already shared" )
963
+ self ._shared_x_axes .join (self , other )
964
+ self ._sharex = other
965
+ self .xaxis .major = other .xaxis .major # Ticker instances holding
966
+ self .xaxis .minor = other .xaxis .minor # locator and formatter.
967
+ x0 , x1 = other .get_xlim ()
968
+ self .set_xlim (x0 , x1 , emit = False , auto = other .get_autoscalex_on ())
969
+ self .xaxis ._scale = other .xaxis ._scale
970
+
971
+ def sharey (self , other ):
972
+ """
973
+ Share the y-axis with *other*.
974
+
975
+ This is equivalent to passing ``sharey=other`` when constructing the
976
+ axes, and cannot be used if the y-axis is already being shared with
977
+ another axes.
978
+ """
979
+ cbook ._check_isinstance (_AxesBase , other = other )
980
+ if self ._sharey is not None and other is not self ._sharey :
981
+ raise ValueError ("y-axis is already shared" )
982
+ self ._shared_y_axes .join (self , other )
983
+ self ._sharey = other
984
+ self .yaxis .major = other .yaxis .major # Ticker instances holding
985
+ self .yaxis .minor = other .yaxis .minor # locator and formatter.
986
+ y0 , y1 = other .get_ylim ()
987
+ self .set_ylim (y0 , y1 , emit = False , auto = other .get_autoscaley_on ())
988
+ self .yaxis ._scale = other .yaxis ._scale
989
+
955
990
def cla (self ):
956
991
"""Clear the current axes."""
957
992
# Note: this is called by Axes.__init__()
@@ -975,38 +1010,25 @@ def cla(self):
975
1010
self .callbacks = cbook .CallbackRegistry ()
976
1011
977
1012
if self ._sharex is not None :
978
- # major and minor are axis.Ticker class instances with
979
- # locator and formatter attributes
980
- self .xaxis .major = self ._sharex .xaxis .major
981
- self .xaxis .minor = self ._sharex .xaxis .minor
982
- x0 , x1 = self ._sharex .get_xlim ()
983
- self .set_xlim (x0 , x1 , emit = False ,
984
- auto = self ._sharex .get_autoscalex_on ())
985
- self .xaxis ._scale = self ._sharex .xaxis ._scale
1013
+ self .sharex (self ._sharex )
986
1014
else :
987
1015
self .xaxis ._set_scale ('linear' )
988
1016
try :
989
1017
self .set_xlim (0 , 1 )
990
1018
except TypeError :
991
1019
pass
992
-
993
1020
if self ._sharey is not None :
994
- self .yaxis .major = self ._sharey .yaxis .major
995
- self .yaxis .minor = self ._sharey .yaxis .minor
996
- y0 , y1 = self ._sharey .get_ylim ()
997
- self .set_ylim (y0 , y1 , emit = False ,
998
- auto = self ._sharey .get_autoscaley_on ())
999
- self .yaxis ._scale = self ._sharey .yaxis ._scale
1021
+ self .sharey (self ._sharey )
1000
1022
else :
1001
1023
self .yaxis ._set_scale ('linear' )
1002
1024
try :
1003
1025
self .set_ylim (0 , 1 )
1004
1026
except TypeError :
1005
1027
pass
1028
+
1006
1029
# update the minor locator for x and y axis based on rcParams
1007
1030
if rcParams ['xtick.minor.visible' ]:
1008
1031
self .xaxis .set_minor_locator (mticker .AutoMinorLocator ())
1009
-
1010
1032
if rcParams ['ytick.minor.visible' ]:
1011
1033
self .yaxis .set_minor_locator (mticker .AutoMinorLocator ())
1012
1034
@@ -1088,11 +1110,10 @@ def cla(self):
1088
1110
1089
1111
self ._shared_x_axes .clean ()
1090
1112
self ._shared_y_axes .clean ()
1091
- if self ._sharex :
1113
+ if self ._sharex is not None :
1092
1114
self .xaxis .set_visible (xaxis_visible )
1093
1115
self .patch .set_visible (patch_visible )
1094
-
1095
- if self ._sharey :
1116
+ if self ._sharey is not None :
1096
1117
self .yaxis .set_visible (yaxis_visible )
1097
1118
self .patch .set_visible (patch_visible )
1098
1119
0 commit comments