@@ -664,6 +664,7 @@ def __init__(self, axes, pickradius=15):
664664 # Initialize here for testing; later add API
665665 self ._major_tick_kw = dict ()
666666 self ._minor_tick_kw = dict ()
667+ self ._tick_space = None
667668
668669 self .cla ()
669670 self ._set_scale ('linear' )
@@ -795,6 +796,7 @@ def set_tick_params(self, which='major', reset=False, **kw):
795796 for tick in self .minorTicks :
796797 tick ._apply_params (** self ._minor_tick_kw )
797798 self .stale = True
799+ self ._tick_space = None
798800
799801 @staticmethod
800802 def _translate_tick_kw (kw , to_init_kw = True ):
@@ -1663,6 +1665,13 @@ def axis_date(self, tz=None):
16631665 tz = pytz .timezone (tz )
16641666 self .update_units (datetime .datetime (2009 , 1 , 1 , 0 , 0 , 0 , 0 , tz ))
16651667
1668+ def get_tick_space (self ):
1669+ """
1670+ Return the estimated number of ticks that can fit on the axis.
1671+ """
1672+ # Must be overridden in the subclass
1673+ raise NotImplementedError ()
1674+
16661675
16671676class XAxis (Axis ):
16681677 __name__ = 'xaxis'
@@ -1986,6 +1995,18 @@ def set_default_intervals(self):
19861995 self .axes .viewLim .intervalx = xmin , xmax
19871996 self .stale = True
19881997
1998+ def get_tick_space (self ):
1999+ if self ._tick_space is None :
2000+ ends = self .axes .transAxes .transform ([[0 , 0 ], [1 , 0 ]])
2001+ length = ((ends [1 ][0 ] - ends [0 ][0 ]) / self .axes .figure .dpi ) * 72.0
2002+ tick = self ._get_tick (True )
2003+ # There is a heuristic here that the aspect ratio of tick text
2004+ # is no more than 3:1
2005+ size = tick .label1 .get_size () * 3
2006+ size *= np .cos (np .deg2rad (tick .label1 .get_rotation ()))
2007+ self ._tick_space = np .floor (length / size )
2008+ return self ._tick_space
2009+
19892010
19902011class YAxis (Axis ):
19912012 __name__ = 'yaxis'
@@ -2316,3 +2337,14 @@ def set_default_intervals(self):
23162337 if not viewMutated :
23172338 self .axes .viewLim .intervaly = ymin , ymax
23182339 self .stale = True
2340+
2341+ def get_tick_space (self ):
2342+ if self ._tick_space is None :
2343+ ends = self .axes .transAxes .transform ([[0 , 0 ], [0 , 1 ]])
2344+ length = ((ends [1 ][1 ] - ends [0 ][1 ]) / self .axes .figure .dpi ) * 72.0
2345+ tick = self ._get_tick (True )
2346+ # Having a spacing of at least 2 just looks good.
2347+ size = tick .label1 .get_size () * 2.0
2348+ size *= np .cos (np .deg2rad (tick .label1 .get_rotation ()))
2349+ self ._tick_space = np .floor (length / size )
2350+ return self ._tick_space
0 commit comments