@@ -470,6 +470,13 @@ def __init__(self, fig, rect,
470
470
to share the x-axis with
471
471
*sharey* an class:`~matplotlib.axes.Axes` instance
472
472
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`.
473
480
*title* the title string
474
481
*visible* [ *True* | *False* ] whether the axes is
475
482
visible
@@ -499,6 +506,10 @@ def __init__(self, fig, rect,
499
506
self .set_anchor ('C' )
500
507
self ._sharex = sharex
501
508
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 )
502
513
if sharex is not None :
503
514
self ._shared_x_axes .join (self , sharex )
504
515
if sharex ._adjustable == 'box' :
@@ -974,50 +985,52 @@ def cla(self):
974
985
self .callbacks = cbook .CallbackRegistry ()
975
986
976
987
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
981
995
x0 , x1 = self ._sharex .get_xlim ()
982
996
self .set_xlim (x0 , x1 , emit = False , auto = None )
983
997
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
-
990
998
# This overwrites the current formatter/locator
991
999
self .xaxis ._set_scale (self ._sharex .xaxis .get_scale ())
992
1000
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 )
998
1009
else :
999
1010
self .xaxis ._set_scale ('linear' )
1000
1011
1001
1012
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
1004
1020
y0 , y1 = self ._sharey .get_ylim ()
1005
1021
self .set_ylim (y0 , y1 , emit = False , auto = None )
1006
1022
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
-
1013
1023
# This overwrites the current formatter/locator
1014
1024
self .yaxis ._set_scale (self ._sharey .yaxis .get_scale ())
1015
1025
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 )
1021
1034
else :
1022
1035
self .yaxis ._set_scale ('linear' )
1023
1036
@@ -3894,21 +3907,27 @@ def _make_twin_axes(self, *kl, **kwargs):
3894
3907
ax2 = self .figure .add_axes (self .get_position (True ), * kl , ** kwargs )
3895
3908
return ax2
3896
3909
3897
- def twinx (self ):
3910
+ def twinx (self , share_tickers = True ):
3898
3911
"""
3899
3912
Create a twin Axes sharing the xaxis
3900
3913
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
3902
3915
x-axis but independent y axis. The y-axis of self will have
3903
3916
ticks on left and the returned axes will have ticks on the
3904
3917
right. To ensure tick marks of both axis align, see
3905
3918
:class:`~matplotlib.ticker.LinearLocator`
3906
3919
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
+
3907
3926
.. note::
3908
3927
For those who are 'picking' artists while using twinx, pick
3909
3928
events are only called for the artists in the top-most axes.
3910
3929
"""
3911
- ax2 = self ._make_twin_axes (sharex = self )
3930
+ ax2 = self ._make_twin_axes (sharex = self , share_tickers = share_tickers )
3912
3931
ax2 .yaxis .tick_right ()
3913
3932
ax2 .yaxis .set_label_position ('right' )
3914
3933
ax2 .yaxis .set_offset_position ('right' )
@@ -3917,21 +3936,27 @@ def twinx(self):
3917
3936
ax2 .patch .set_visible (False )
3918
3937
return ax2
3919
3938
3920
- def twiny (self ):
3939
+ def twiny (self , share_tickers = True ):
3921
3940
"""
3922
3941
Create a twin Axes sharing the yaxis
3923
3942
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
3925
3944
y-axis but independent x axis. The x-axis of self will have
3926
3945
ticks on bottom and the returned axes will have ticks on the
3927
3946
top.
3928
3947
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
+
3929
3954
.. note::
3930
3955
For those who are 'picking' artists while using twiny, pick
3931
3956
events are only called for the artists in the top-most axes.
3932
3957
"""
3933
3958
3934
- ax2 = self ._make_twin_axes (sharey = self )
3959
+ ax2 = self ._make_twin_axes (sharey = self , share_tickers = share_tickers )
3935
3960
ax2 .xaxis .tick_top ()
3936
3961
ax2 .xaxis .set_label_position ('top' )
3937
3962
self .xaxis .tick_bottom ()
0 commit comments