Skip to content

Commit b0ac453

Browse files
committed
Ensure polar axis labels see both tick directions
Since these labels are currently just an x/y analog (and not tied to the axis orientation), they need to "see" the other axis in order to avoid overlap.
1 parent cd46c10 commit b0ac453

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

lib/matplotlib/projections/polar.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,30 @@ class ThetaAxis(maxis.XAxis):
426426
axis_name = 'theta' #: Read-only name identifying the axis.
427427
_tick_class = ThetaTick
428428

429+
def _update_label_position(self, renderer):
430+
"""
431+
Update the label position based on the bounding box enclosing
432+
all the ticklabels and axis spine
433+
"""
434+
if not self._autolabelpos:
435+
return
436+
437+
# get bounding boxes for this axis and any siblings
438+
# that have been set by `fig.align_xlabels()`
439+
xbboxes, xbboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
440+
ybboxes, ybboxes2 = self.axes.yaxis._get_tick_boxes_siblings(renderer=renderer)
441+
# Union with extents of the bottom spine if present, of the axes otherwise.
442+
bbox = mtransforms.Bbox.union([
443+
*xbboxes, *xbboxes2, *ybboxes, *ybboxes2,
444+
self.axes.spines.get(self.label_position, self.axes).get_window_extent()])
445+
446+
x, y = self.label.get_position()
447+
if self.label_position == 'bottom':
448+
y = bbox.y0 - self.labelpad * self.get_figure(root=True).dpi / 72
449+
else:
450+
y = bbox.y1 + self.labelpad * self.get_figure(root=True).dpi / 72
451+
self.label.set_position((x, y))
452+
429453
def _wrap_locator_formatter(self):
430454
self.set_major_locator(ThetaLocator(self.get_major_locator()))
431455
self.set_major_formatter(ThetaFormatter())
@@ -719,6 +743,30 @@ def __init__(self, *args, **kwargs):
719743
super().__init__(*args, **kwargs)
720744
self.sticky_edges.y.append(0)
721745

746+
def _update_label_position(self, renderer):
747+
"""
748+
Update the label position based on the bounding box enclosing
749+
all the ticklabels and axis spine
750+
"""
751+
if not self._autolabelpos:
752+
return
753+
754+
# get bounding boxes for this axis and any siblings
755+
# that have been set by `fig.align_xlabels()`
756+
xbboxes, xbboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
757+
ybboxes, ybboxes2 = self.axes.xaxis._get_tick_boxes_siblings(renderer=renderer)
758+
# Union with extents of the linked spine if present, of the axes otherwise.
759+
bbox = mtransforms.Bbox.union([
760+
*xbboxes, *xbboxes2, *ybboxes, *ybboxes2,
761+
self.axes.spines.get(self.label_position, self.axes).get_window_extent()])
762+
763+
x, y = self.label.get_position()
764+
if self.label_position == 'left':
765+
x = bbox.x0 - self.labelpad * self.get_figure(root=True).dpi / 72
766+
else:
767+
x = bbox.x1 + self.labelpad * self.get_figure(root=True).dpi / 72
768+
self.label.set_position((x, y))
769+
722770
def _wrap_locator_formatter(self):
723771
self.set_major_locator(RadialLocator(self.get_major_locator(),
724772
self.axes))

0 commit comments

Comments
 (0)