Skip to content

Commit

Permalink
Uncamelcase some internal variables in axis.py; rename _get_tick_bboxes.
Browse files Browse the repository at this point in the history
It's actually returning the ticklabel bboxes, not including the
tickmarks themselves.
  • Loading branch information
anntzer committed Oct 20, 2021
1 parent 9fb5370 commit 4ff16a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
64 changes: 27 additions & 37 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,24 +1018,18 @@ def _set_artist_props(self, a):
a.set_figure(self.figure)

def get_ticklabel_extents(self, renderer):
"""
Get the extents of the tick labels on either side
of the axes.
"""

"""Get the extents of the tick labels on either side of the axes."""
ticks_to_draw = self._update_ticks()
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
renderer)

if len(ticklabelBoxes):
bbox = mtransforms.Bbox.union(ticklabelBoxes)
tlb1, tlb2 = self._get_ticklabel_bboxes(ticks_to_draw, renderer)
if len(tlb1):
bbox1 = mtransforms.Bbox.union(tlb1)
else:
bbox = mtransforms.Bbox.from_extents(0, 0, 0, 0)
if len(ticklabelBoxes2):
bbox2 = mtransforms.Bbox.union(ticklabelBoxes2)
bbox1 = mtransforms.Bbox.from_extents(0, 0, 0, 0)
if len(tlb2):
bbox2 = mtransforms.Bbox.union(tlb2)
else:
bbox2 = mtransforms.Bbox.from_extents(0, 0, 0, 0)
return bbox, bbox2
return bbox1, bbox2

def _update_ticks(self):
"""
Expand Down Expand Up @@ -1080,7 +1074,7 @@ def _update_ticks(self):

return ticks_to_draw

def _get_tick_bboxes(self, ticks, renderer):
def _get_ticklabel_bboxes(self, ticks, renderer):
"""Return lists of bboxes for ticks' label1's and label2's."""
return ([tick.label1.get_window_extent(renderer)
for tick in ticks if tick.label1.get_visible()],
Expand All @@ -1105,18 +1099,16 @@ def get_tightbbox(self, renderer, *, for_layout_only=False):
self._update_label_position(renderer)

# go back to just this axis's tick labels
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(
ticks_to_draw, renderer)
tlb1, tlb2 = self._get_tick_bboxes(ticks_to_draw, renderer)

self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2)
self._update_offset_text_position(tlb1, tlb2)
self.offsetText.set_text(self.major.formatter.get_offset())

bboxes = [
*(a.get_window_extent(renderer)
for a in [self.offsetText]
if a.get_visible()),
*ticklabelBoxes,
*ticklabelBoxes2,
*tlb1, *tlb2,
]
# take care of label
if self.label.get_visible():
Expand Down Expand Up @@ -1156,22 +1148,20 @@ def draw(self, renderer, *args, **kwargs):
renderer.open_group(__name__, gid=self.get_gid())

ticks_to_draw = self._update_ticks()
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
renderer)
tlb1, tlb2 = self._get_tick_bboxes(ticks_to_draw, renderer)

for tick in ticks_to_draw:
tick.draw(renderer)

# scale up the axis label box to also find the neighbors, not
# just the tick labels that actually overlap note we need a
# *copy* of the axis label box because we don't want to scale
# the actual bbox
# scale up the axis label box to also find the neighbors, not just the
# tick labels that actually overlap note we need a *copy* of the axis
# label box because we don't want to scale the actual bbox

self._update_label_position(renderer)

self.label.draw(renderer)

self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2)
self._update_offset_text_position(tlb1, tlb2)
self.offsetText.set_text(self.major.formatter.get_offset())
self.offsetText.draw(renderer)

Expand Down Expand Up @@ -2144,19 +2134,19 @@ def get_text_heights(self, renderer):
"""
bbox, bbox2 = self.get_ticklabel_extents(renderer)
# MGDTODO: Need a better way to get the pad
padPixels = self.majorTicks[0].get_pad_pixels()
pad_px = self.majorTicks[0].get_pad_pixels()

above = 0.0
if bbox2.height:
above += bbox2.height + padPixels
above += bbox2.height + pad_px
below = 0.0
if bbox.height:
below += bbox.height + padPixels
below += bbox.height + pad_px

if self.get_label_position() == 'top':
above += self.label.get_window_extent(renderer).height + padPixels
above += self.label.get_window_extent(renderer).height + pad_px
else:
below += self.label.get_window_extent(renderer).height + padPixels
below += self.label.get_window_extent(renderer).height + pad_px
return above, below

def set_ticks_position(self, position):
Expand Down Expand Up @@ -2408,19 +2398,19 @@ def set_offset_position(self, position):
def get_text_widths(self, renderer):
bbox, bbox2 = self.get_ticklabel_extents(renderer)
# MGDTODO: Need a better way to get the pad
padPixels = self.majorTicks[0].get_pad_pixels()
pad_px = self.majorTicks[0].get_pad_pixels()

left = 0.0
if bbox.width:
left += bbox.width + padPixels
left += bbox.width + pad_px
right = 0.0
if bbox2.width:
right += bbox2.width + padPixels
right += bbox2.width + pad_px

if self.get_label_position() == 'left':
left += self.label.get_window_extent(renderer).width + padPixels
left += self.label.get_window_extent(renderer).width + pad_px
else:
right += self.label.get_window_extent(renderer).width + padPixels
right += self.label.get_window_extent(renderer).width + pad_px
return left, right

def set_ticks_position(self, position):
Expand Down
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/mplot3d/axis3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def get_tightbbox(self, renderer, *, for_layout_only=False):

ticks = ticks_to_draw

bb_1, bb_2 = self._get_tick_bboxes(ticks, renderer)
bb_1, bb_2 = self._get_ticklabel_bboxes(ticks, renderer)
other = []

if self.line.get_visible():
Expand Down

0 comments on commit 4ff16a1

Please sign in to comment.