@@ -470,6 +470,13 @@ def __init__(self, fig, rect,
470470 to share the x-axis with
471471 *sharey* an class:`~matplotlib.axes.Axes` instance
472472 to share the y-axis with
473+ *share_tickers* [ *True* | *False* ] whether the major and
474+ minor `Formatter` and `Locator` instances
475+ are always shared (if `True`) or can be set
476+ independently (if `False`) between this set
477+ of axes and `sharex` and `sharey`. This
478+ argument has no meaning if neither `sharex`
479+ nor `sharey` are set. Defaults to `True`.
473480 *title* the title string
474481 *visible* [ *True* | *False* ] whether the axes is
475482 visible
@@ -499,6 +506,10 @@ def __init__(self, fig, rect,
499506 self .set_anchor ('C' )
500507 self ._sharex = sharex
501508 self ._sharey = sharey
509+ # share_tickers is only used as a modifier for sharex/y. It
510+ # should not remain in kwargs by the time kwargs updates the
511+ # instance dictionary.
512+ self ._share_tickers = kwargs .pop ('share_tickers' , True )
502513 if sharex is not None :
503514 self ._shared_x_axes .join (self , sharex )
504515 if sharex ._adjustable == 'box' :
@@ -974,50 +985,52 @@ def cla(self):
974985 self .callbacks = cbook .CallbackRegistry ()
975986
976987 if self ._sharex is not None :
977- # major and minor are class instances with
978- # locator and formatter attributes
979- self .xaxis .major = self ._sharex .xaxis .major
980- self .xaxis .minor = self ._sharex .xaxis .minor
988+ # The tickers need to exist but can be empty until after the
989+ # call to Axis._set_scale since they will be overwritten
990+ # anyway
991+ self .xaxis .major = maxis .Ticker ()
992+ self .xaxis .minor = maxis .Ticker ()
993+
994+ # Copy the axis limits
981995 x0 , x1 = self ._sharex .get_xlim ()
982996 self .set_xlim (x0 , x1 , emit = False , auto = None )
983997
984- # Save the current formatter/locator so we don't lose it
985- majf = self ._sharex .xaxis .get_major_formatter ()
986- minf = self ._sharex .xaxis .get_minor_formatter ()
987- majl = self ._sharex .xaxis .get_major_locator ()
988- minl = self ._sharex .xaxis .get_minor_locator ()
989-
990998 # This overwrites the current formatter/locator
991999 self .xaxis ._set_scale (self ._sharex .xaxis .get_scale ())
9921000
993- # Reset the formatter/locator
994- self .xaxis .set_major_formatter (majf )
995- self .xaxis .set_minor_formatter (minf )
996- self .xaxis .set_major_locator (majl )
997- self .xaxis .set_minor_locator (minl )
1001+ # Reset the formatter/locator. Axis handle gets marked as
1002+ # stale in previous line, no need to repeat.
1003+ if self ._share_tickers :
1004+ self .xaxis .major = self ._sharex .xaxis .major
1005+ self .xaxis .minor = self ._sharex .xaxis .minor
1006+ else :
1007+ self .xaxis .major = maxis .Ticker (self ._sharex .xaxis .major )
1008+ self .xaxis .minor = maxis .Ticker (self ._sharex .xaxis .minor )
9981009 else :
9991010 self .xaxis ._set_scale ('linear' )
10001011
10011012 if self ._sharey is not None :
1002- self .yaxis .major = self ._sharey .yaxis .major
1003- self .yaxis .minor = self ._sharey .yaxis .minor
1013+ # The tickers need to exist but can be empty until after the
1014+ # call to Axis._set_scale since they will be overwritten
1015+ # anyway
1016+ self .yaxis .major = maxis .Ticker ()
1017+ self .yaxis .minor = maxis .Ticker ()
1018+
1019+ # Copy the axis limits
10041020 y0 , y1 = self ._sharey .get_ylim ()
10051021 self .set_ylim (y0 , y1 , emit = False , auto = None )
10061022
1007- # Save the current formatter/locator so we don't lose it
1008- majf = self ._sharey .yaxis .get_major_formatter ()
1009- minf = self ._sharey .yaxis .get_minor_formatter ()
1010- majl = self ._sharey .yaxis .get_major_locator ()
1011- minl = self ._sharey .yaxis .get_minor_locator ()
1012-
10131023 # This overwrites the current formatter/locator
10141024 self .yaxis ._set_scale (self ._sharey .yaxis .get_scale ())
10151025
1016- # Reset the formatter/locator
1017- self .yaxis .set_major_formatter (majf )
1018- self .yaxis .set_minor_formatter (minf )
1019- self .yaxis .set_major_locator (majl )
1020- self .yaxis .set_minor_locator (minl )
1026+ # Reset the formatter/locator. Axis handle gets marked as
1027+ # stale in previous line, no need to repeat.
1028+ if self ._share_tickers :
1029+ self .yaxis .major = self ._sharey .yaxis .major
1030+ self .yaxis .minor = self ._sharey .yaxis .minor
1031+ else :
1032+ self .yaxis .major = maxis .Ticker (self ._sharey .yaxis .major )
1033+ self .yaxis .minor = maxis .Ticker (self ._sharey .yaxis .minor )
10211034 else :
10221035 self .yaxis ._set_scale ('linear' )
10231036
@@ -3894,21 +3907,27 @@ def _make_twin_axes(self, *kl, **kwargs):
38943907 ax2 = self .figure .add_axes (self .get_position (True ), * kl , ** kwargs )
38953908 return ax2
38963909
3897- def twinx (self ):
3910+ def twinx (self , share_tickers = True ):
38983911 """
38993912 Create a twin Axes sharing the xaxis
39003913
3901- create a twin of Axes for generating a plot with a sharex
3914+ Create a twin of Axes for generating a plot with a sharex
39023915 x-axis but independent y axis. The y-axis of self will have
39033916 ticks on left and the returned axes will have ticks on the
39043917 right. To ensure tick marks of both axis align, see
39053918 :class:`~matplotlib.ticker.LinearLocator`
39063919
3920+ `share_tickers` determines if the shared axis will always have
3921+ the same major and minor `Formatter` and `Locator` objects as
3922+ this one. This is usually desirable since the axes overlap.
3923+ However, if one of the axes is shifted so that they are both
3924+ visible, it may be useful to set this parameter to ``False``.
3925+
39073926 .. note::
39083927 For those who are 'picking' artists while using twinx, pick
39093928 events are only called for the artists in the top-most axes.
39103929 """
3911- ax2 = self ._make_twin_axes (sharex = self )
3930+ ax2 = self ._make_twin_axes (sharex = self , share_tickers = share_tickers )
39123931 ax2 .yaxis .tick_right ()
39133932 ax2 .yaxis .set_label_position ('right' )
39143933 ax2 .yaxis .set_offset_position ('right' )
@@ -3917,21 +3936,27 @@ def twinx(self):
39173936 ax2 .patch .set_visible (False )
39183937 return ax2
39193938
3920- def twiny (self ):
3939+ def twiny (self , share_tickers = True ):
39213940 """
39223941 Create a twin Axes sharing the yaxis
39233942
3924- create a twin of Axes for generating a plot with a shared
3943+ Create a twin of Axes for generating a plot with a shared
39253944 y-axis but independent x axis. The x-axis of self will have
39263945 ticks on bottom and the returned axes will have ticks on the
39273946 top.
39283947
3948+ `share_tickers` determines if the shared axis will always have
3949+ the same major and minor `Formatter` and `Locator` objects as
3950+ this one. This is usually desirable since the axes overlap.
3951+ However, if one of the axes is shifted so that they are both
3952+ visible, it may be useful to set this parameter to ``False``.
3953+
39293954 .. note::
39303955 For those who are 'picking' artists while using twiny, pick
39313956 events are only called for the artists in the top-most axes.
39323957 """
39333958
3934- ax2 = self ._make_twin_axes (sharey = self )
3959+ ax2 = self ._make_twin_axes (sharey = self , share_tickers = share_tickers )
39353960 ax2 .xaxis .tick_top ()
39363961 ax2 .xaxis .set_label_position ('top' )
39373962 self .xaxis .tick_bottom ()
0 commit comments