@@ -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,46 @@ 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
+ if not isinstance (sharex , _AxesBase ):
961
+ raise TypeError ('sharex must be an axes, not a bool' )
962
+ if self ._sharex is not None and other is not self ._sharex :
963
+ raise ValueError ("x-axis is already shared" )
964
+ self ._shared_x_axes .join (self , other )
965
+ self ._sharex = other
966
+ self .xaxis .major = other .xaxis .major # Ticker instances holding
967
+ self .xaxis .minor = other .xaxis .minor # locator and formatter.
968
+ x0 , x1 = other .get_xlim ()
969
+ self .set_xlim (x0 , x1 , emit = False , auto = other .get_autoscalex_on ())
970
+ self .xaxis ._scale = other .xaxis ._scale
971
+
972
+ def sharey (self , other ):
973
+ """
974
+ Share the y-axis with *other*.
975
+
976
+ This is equivalent to passing ``sharey=other`` when constructing the
977
+ axes, and cannot be used if the y-axis is already being shared with
978
+ another axes.
979
+ """
980
+ if not isinstance (sharey , _AxesBase ):
981
+ raise TypeError ('sharey must be an axes, not a bool' )
982
+ if self ._sharey is not None and other is not self ._sharey :
983
+ raise ValueError ("y-axis is already shared" )
984
+ self ._shared_y_axes .join (self , other )
985
+ self ._sharey = other
986
+ self .yaxis .major = other .yaxis .major # Ticker instances holding
987
+ self .yaxis .minor = other .yaxis .minor # locator and formatter.
988
+ y0 , y1 = other .get_ylim ()
989
+ self .set_ylim (y0 , y1 , emit = False , auto = other .get_autoscaley_on ())
990
+ self .yaxis ._scale = other .yaxis ._scale
991
+
955
992
def cla (self ):
956
993
"""Clear the current axes."""
957
994
# Note: this is called by Axes.__init__()
@@ -975,38 +1012,25 @@ def cla(self):
975
1012
self .callbacks = cbook .CallbackRegistry ()
976
1013
977
1014
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
1015
+ self .sharex (self ._sharex )
986
1016
else :
987
1017
self .xaxis ._set_scale ('linear' )
988
1018
try :
989
1019
self .set_xlim (0 , 1 )
990
1020
except TypeError :
991
1021
pass
992
-
993
1022
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
1023
+ self .sharey (self ._sharey )
1000
1024
else :
1001
1025
self .yaxis ._set_scale ('linear' )
1002
1026
try :
1003
1027
self .set_ylim (0 , 1 )
1004
1028
except TypeError :
1005
1029
pass
1030
+
1006
1031
# update the minor locator for x and y axis based on rcParams
1007
1032
if rcParams ['xtick.minor.visible' ]:
1008
1033
self .xaxis .set_minor_locator (mticker .AutoMinorLocator ())
1009
-
1010
1034
if rcParams ['ytick.minor.visible' ]:
1011
1035
self .yaxis .set_minor_locator (mticker .AutoMinorLocator ())
1012
1036
@@ -1088,11 +1112,10 @@ def cla(self):
1088
1112
1089
1113
self ._shared_x_axes .clean ()
1090
1114
self ._shared_y_axes .clean ()
1091
- if self ._sharex :
1115
+ if self ._sharex is not None :
1092
1116
self .xaxis .set_visible (xaxis_visible )
1093
1117
self .patch .set_visible (patch_visible )
1094
-
1095
- if self ._sharey :
1118
+ if self ._sharey is not None :
1096
1119
self .yaxis .set_visible (yaxis_visible )
1097
1120
self .patch .set_visible (patch_visible )
1098
1121
0 commit comments