@@ -654,6 +654,7 @@ def __init__(self, axes, pickradius=15):
654654 # Initialize here for testing; later add API
655655 self ._major_tick_kw = dict ()
656656 self ._minor_tick_kw = dict ()
657+ self ._tick_space = None
657658
658659 self .cla ()
659660 self ._set_scale ('linear' )
@@ -785,6 +786,7 @@ def set_tick_params(self, which='major', reset=False, **kw):
785786 for tick in self .minorTicks :
786787 tick ._apply_params (** self ._minor_tick_kw )
787788 self .stale = True
789+ self ._tick_space = None
788790
789791 @staticmethod
790792 def _translate_tick_kw (kw , to_init_kw = True ):
@@ -1665,6 +1667,13 @@ def axis_date(self, tz=None):
16651667 tz = pytz .timezone (tz )
16661668 self .update_units (datetime .datetime (2009 , 1 , 1 , 0 , 0 , 0 , 0 , tz ))
16671669
1670+ def get_tick_space (self ):
1671+ """
1672+ Return the estimated number of ticks that can fit on the axis.
1673+ """
1674+ # Must be overridden in the subclass
1675+ raise NotImplementedError ()
1676+
16681677
16691678class XAxis (Axis ):
16701679 __name__ = 'xaxis'
@@ -1988,6 +1997,18 @@ def set_default_intervals(self):
19881997 self .axes .viewLim .intervalx = xmin , xmax
19891998 self .stale = True
19901999
2000+ def get_tick_space (self ):
2001+ if self ._tick_space is None :
2002+ ends = self .axes .transAxes .transform ([[0 , 0 ], [1 , 0 ]])
2003+ length = ((ends [1 ][0 ] - ends [0 ][0 ]) / self .axes .figure .dpi ) * 72.0
2004+ tick = self ._get_tick (True )
2005+ # There is a heuristic here that the aspect ratio of tick text
2006+ # is no more than 3:1
2007+ size = tick .label1 .get_size () * 3
2008+ size *= np .cos (np .deg2rad (tick .label1 .get_rotation ()))
2009+ self ._tick_space = np .floor (length / size )
2010+ return self ._tick_space
2011+
19912012
19922013class YAxis (Axis ):
19932014 __name__ = 'yaxis'
@@ -2318,3 +2339,14 @@ def set_default_intervals(self):
23182339 if not viewMutated :
23192340 self .axes .viewLim .intervaly = ymin , ymax
23202341 self .stale = True
2342+
2343+ def get_tick_space (self ):
2344+ if self ._tick_space is None :
2345+ ends = self .axes .transAxes .transform ([[0 , 0 ], [0 , 1 ]])
2346+ length = ((ends [1 ][1 ] - ends [0 ][1 ]) / self .axes .figure .dpi ) * 72.0
2347+ tick = self ._get_tick (True )
2348+ # Having a spacing of at least 2 just looks good.
2349+ size = tick .label1 .get_size () * 2.0
2350+ size *= np .cos (np .deg2rad (tick .label1 .get_rotation ()))
2351+ self ._tick_space = np .floor (length / size )
2352+ return self ._tick_space
0 commit comments