Skip to content

Commit

Permalink
Fix overlapping ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Sep 29, 2023
1 parent ad78d38 commit e1bc54e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 9 additions & 1 deletion astropy/visualization/wcsaxes/coordinate_helpers.py
Expand Up @@ -628,14 +628,22 @@ def _draw_grid(self, renderer):

renderer.close_group("grid lines")

def _draw_ticks(self, renderer):
def _draw_ticks(self, renderer, existing_bboxes):
"""
Draw all ticks and ticklabels.
Parameters
----------
existing_bboxes : list[Bbox]
All bboxes for ticks that have already been drawn by other
coordinates.
"""
renderer.open_group("ticks")
self.ticks.draw(renderer)

self.ticklabels._tick_out_size = self.ticks.out_size

self.ticklabels._set_existing_bboxes(existing_bboxes)
self.ticklabels.draw(renderer)

renderer.close_group("ticks")
Expand Down
2 changes: 1 addition & 1 deletion astropy/visualization/wcsaxes/core.py
Expand Up @@ -511,7 +511,7 @@ def draw_wcsaxes(self, renderer):
for coords in self._all_coords:
# Draw tick labels
for coord in coords:
coord._draw_ticks(renderer)
coord._draw_ticks(renderer, self._bboxes)

visible_ticks.extend(coord.ticklabels.get_visible_axes())
# Save ticklabel bboxes
Expand Down
12 changes: 8 additions & 4 deletions astropy/visualization/wcsaxes/ticklabels.py
Expand Up @@ -26,12 +26,14 @@ def __init__(self, frame, *args, **kwargs):
self.set_pad(rcParams["xtick.major.pad"])
self._exclude_overlapping = False

# Mapping from axis > list[bounding boxes]
self._axis_bboxes = defaultdict(list)

# Stale if either xy positions haven't been calculated, or if
# something changes that requires recomputing the positions
self._stale = True

# Check rcParams

if "color" not in kwargs:
self.set_color(rcParams["xtick.color"])

Expand Down Expand Up @@ -306,9 +308,12 @@ def _all_bboxes(self):
ret += self._axis_bboxes[axis]
return ret

def _set_existing_bboxes(self, bboxes):
self._existing_bboxes = bboxes

@allow_rasterization
def draw(self, renderer, bboxes=None, ticklabels_bbox=None, tick_out_size=None):
# Mapping from axis > list[bounding boxes]
# Reset bounding boxes
self._axis_bboxes = defaultdict(list)

if not self.get_visible():
Expand All @@ -326,10 +331,9 @@ def draw(self, renderer, bboxes=None, ticklabels_bbox=None, tick_out_size=None):
# TODO: the problem here is that we might get rid of a label
# that has a key starting bit such as -0:30 where the -0
# might be dropped from all other labels.

if (
not self._exclude_overlapping
or bb.count_overlaps(self._all_bboxes) == 0
or bb.count_overlaps(self._all_bboxes + self._existing_bboxes) == 0
):
super().draw(renderer)
self._axis_bboxes[axis].append(bb)

0 comments on commit e1bc54e

Please sign in to comment.