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