Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 255f213

Browse files
author
Daniel Hyams
committed
Added a strategy to stop losing ticks on the ends of an axis. The approach that is
used is to pad the interval that is being used to "clip" ticks from the axis because they are off the end. The padding is chosen specifically such that the tick cannot be drawn a pixel past the end of the axis. So long as the tick is within this bound, it should be OK to draw it.
1 parent 034b12b commit 255f213

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

lib/matplotlib/axis.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,11 +972,24 @@ def _update_ticks(self, renderer):
972972
tick_tups = [ti for ti in tick_tups
973973
if (ti[1] >= ilow) and (ti[1] <= ihigh)]
974974

975+
# so that we don't lose ticks on the end, expand out the interval ever so slightly. The
976+
# "ever so slightly" is defined to be the width of a half of a pixel. We don't want to draw
977+
# a tick that even one pixel outside of the defined axis interval.
978+
if interval[0] <= interval[1]:
979+
interval_expanded = interval
980+
else:
981+
interval_expanded = interval[1],interval[0]
982+
983+
if hasattr(self,'_get_pixel_distance_along_axis'):
984+
ds1 = self._get_pixel_distance_along_axis(interval_expanded[0],-1)/2.0
985+
ds2 = self._get_pixel_distance_along_axis(interval_expanded[1],+1)/2.0
986+
interval_expanded = (interval[0]-ds1,interval[1]+ds2)
987+
975988
ticks_to_draw = []
976989
for tick, loc, label in tick_tups:
977990
if tick is None:
978991
continue
979-
if not mtransforms.interval_contains(interval, loc):
992+
if not mtransforms.interval_contains(interval_expanded, loc):
980993
continue
981994
tick.update_position(loc)
982995
tick.set_label1(label)
@@ -1599,6 +1612,24 @@ def _get_offset_text(self):
15991612
self.offset_text_position = 'bottom'
16001613
return offsetText
16011614

1615+
def _get_pixel_distance_along_axis(self,where,perturb):
1616+
# returns the amount, in data coordinates, that a single pixel corresponds to in the
1617+
# locality given by "where", which is also given in data coordinates, and is an x coordinate.
1618+
# "perturb" is the amount to perturb the pixel. Usually +1 or -1.
1619+
1620+
# first figure out the pixel location of the "where" point. We use 1e-10 for the
1621+
# y point, so that we remain compatible with log axes.
1622+
#
1623+
# I THINK this will work too for polar axes, but I'm not 100% sure.
1624+
#
1625+
trans = self.axes.transData # transformation from data coords to display coords
1626+
transinv = trans.inverted() # transformation from display coords to data coords
1627+
pix = trans.transform_point((where,1e-10))
1628+
ptp = transinv.transform_point((pix[0]+perturb,pix[1])) # perturb the pixel.
1629+
dx = abs(ptp[0]-where)
1630+
1631+
return dx
1632+
16021633
def get_label_position(self):
16031634
"""
16041635
Return the label position (top or bottom)
@@ -1874,6 +1905,22 @@ def _get_offset_text(self):
18741905
self.offset_text_position = 'left'
18751906
return offsetText
18761907

1908+
def _get_pixel_distance_along_axis(self,where,perturb):
1909+
# returns the amount, in data coordinates, that a single pixel corresponds to in the
1910+
# locality given by "where", which is also given in data coordinates, and is a y coordinate.
1911+
# "perturb" is the amount to perturb the pixel. Usually +1 or -1.
1912+
1913+
# first figure out the pixel location of the "where" point. We use 1e-10 for the
1914+
# x point, so that we remain compatible with log axes.
1915+
#
1916+
# I THINK this will work too for polar axes, but I'm not 100% sure.
1917+
#
1918+
trans = self.axes.transData # transformation from data coords to display coords
1919+
transinv = trans.inverted() # transformation from display coords to data coords
1920+
pix = trans.transform_point((1e-10,where))
1921+
ptp = transinv.transform_point((pix[0],pix[1]+perturb)) # perturb the pixel.
1922+
dy = abs(ptp[1]-where)
1923+
18771924
def get_label_position(self):
18781925
"""
18791926
Return the label position (left or right)

0 commit comments

Comments
 (0)