@@ -487,16 +487,8 @@ def __init__(self, fig, rect,
487
487
self ._anchor = 'C'
488
488
self ._stale_viewlim_x = False
489
489
self ._stale_viewlim_y = False
490
- self ._sharex = sharex
491
- self ._sharey = sharey
492
- if sharex is not None :
493
- if not isinstance (sharex , _AxesBase ):
494
- raise TypeError ('sharex must be an axes, not a bool' )
495
- self ._shared_x_axes .join (self , sharex )
496
- if sharey is not None :
497
- if not isinstance (sharey , _AxesBase ):
498
- raise TypeError ('sharey must be an axes, not a bool' )
499
- self ._shared_y_axes .join (self , sharey )
490
+ self ._sharex = None
491
+ self ._sharey = None
500
492
self .set_label (label )
501
493
self .set_figure (fig )
502
494
self .set_box_aspect (box_aspect )
@@ -515,6 +507,11 @@ def __init__(self, fig, rect,
515
507
self ._rasterization_zorder = None
516
508
self .cla ()
517
509
510
+ if sharex is not None :
511
+ self .sharex (sharex )
512
+ if sharey is not None :
513
+ self .sharey (sharey )
514
+
518
515
# funcs used to format x and y - fall back on major formatters
519
516
self .fmt_xdata = None
520
517
self .fmt_ydata = None
@@ -1008,6 +1005,44 @@ def _gen_axes_spines(self, locations=None, offset=0.0, units='inches'):
1008
1005
return OrderedDict ((side , mspines .Spine .linear_spine (self , side ))
1009
1006
for side in ['left' , 'right' , 'bottom' , 'top' ])
1010
1007
1008
+ def sharex (self , other ):
1009
+ """
1010
+ Share the x-axis with *other*.
1011
+
1012
+ This is equivalent to passing ``sharex=other`` when constructing the
1013
+ axes, and cannot be used if the x-axis is already being shared with
1014
+ another axes.
1015
+ """
1016
+ cbook ._check_isinstance (_AxesBase , other = other )
1017
+ if self ._sharex is not None and other is not self ._sharex :
1018
+ raise ValueError ("x-axis is already shared" )
1019
+ self ._shared_x_axes .join (self , other )
1020
+ self ._sharex = other
1021
+ self .xaxis .major = other .xaxis .major # Ticker instances holding
1022
+ self .xaxis .minor = other .xaxis .minor # locator and formatter.
1023
+ x0 , x1 = other .get_xlim ()
1024
+ self .set_xlim (x0 , x1 , emit = False , auto = other .get_autoscalex_on ())
1025
+ self .xaxis ._scale = other .xaxis ._scale
1026
+
1027
+ def sharey (self , other ):
1028
+ """
1029
+ Share the y-axis with *other*.
1030
+
1031
+ This is equivalent to passing ``sharey=other`` when constructing the
1032
+ axes, and cannot be used if the y-axis is already being shared with
1033
+ another axes.
1034
+ """
1035
+ cbook ._check_isinstance (_AxesBase , other = other )
1036
+ if self ._sharey is not None and other is not self ._sharey :
1037
+ raise ValueError ("y-axis is already shared" )
1038
+ self ._shared_y_axes .join (self , other )
1039
+ self ._sharey = other
1040
+ self .yaxis .major = other .yaxis .major # Ticker instances holding
1041
+ self .yaxis .minor = other .yaxis .minor # locator and formatter.
1042
+ y0 , y1 = other .get_ylim ()
1043
+ self .set_ylim (y0 , y1 , emit = False , auto = other .get_autoscaley_on ())
1044
+ self .yaxis ._scale = other .yaxis ._scale
1045
+
1011
1046
def cla (self ):
1012
1047
"""Clear the current axes."""
1013
1048
# Note: this is called by Axes.__init__()
@@ -1031,38 +1066,25 @@ def cla(self):
1031
1066
self .callbacks = cbook .CallbackRegistry ()
1032
1067
1033
1068
if self ._sharex is not None :
1034
- # major and minor are axis.Ticker class instances with
1035
- # locator and formatter attributes
1036
- self .xaxis .major = self ._sharex .xaxis .major
1037
- self .xaxis .minor = self ._sharex .xaxis .minor
1038
- x0 , x1 = self ._sharex .get_xlim ()
1039
- self .set_xlim (x0 , x1 , emit = False ,
1040
- auto = self ._sharex .get_autoscalex_on ())
1041
- self .xaxis ._scale = self ._sharex .xaxis ._scale
1069
+ self .sharex (self ._sharex )
1042
1070
else :
1043
1071
self .xaxis ._set_scale ('linear' )
1044
1072
try :
1045
1073
self .set_xlim (0 , 1 )
1046
1074
except TypeError :
1047
1075
pass
1048
-
1049
1076
if self ._sharey is not None :
1050
- self .yaxis .major = self ._sharey .yaxis .major
1051
- self .yaxis .minor = self ._sharey .yaxis .minor
1052
- y0 , y1 = self ._sharey .get_ylim ()
1053
- self .set_ylim (y0 , y1 , emit = False ,
1054
- auto = self ._sharey .get_autoscaley_on ())
1055
- self .yaxis ._scale = self ._sharey .yaxis ._scale
1077
+ self .sharey (self ._sharey )
1056
1078
else :
1057
1079
self .yaxis ._set_scale ('linear' )
1058
1080
try :
1059
1081
self .set_ylim (0 , 1 )
1060
1082
except TypeError :
1061
1083
pass
1084
+
1062
1085
# update the minor locator for x and y axis based on rcParams
1063
1086
if mpl .rcParams ['xtick.minor.visible' ]:
1064
1087
self .xaxis .set_minor_locator (mticker .AutoMinorLocator ())
1065
-
1066
1088
if mpl .rcParams ['ytick.minor.visible' ]:
1067
1089
self .yaxis .set_minor_locator (mticker .AutoMinorLocator ())
1068
1090
@@ -1144,11 +1166,10 @@ def cla(self):
1144
1166
1145
1167
self ._shared_x_axes .clean ()
1146
1168
self ._shared_y_axes .clean ()
1147
- if self ._sharex :
1169
+ if self ._sharex is not None :
1148
1170
self .xaxis .set_visible (xaxis_visible )
1149
1171
self .patch .set_visible (patch_visible )
1150
-
1151
- if self ._sharey :
1172
+ if self ._sharey is not None :
1152
1173
self .yaxis .set_visible (yaxis_visible )
1153
1174
self .patch .set_visible (patch_visible )
1154
1175
0 commit comments