Skip to content

Commit

Permalink
Merge pull request matplotlib#9881 from QuLogic/polar-tick-fixes
Browse files Browse the repository at this point in the history
Polar tick fixes
  • Loading branch information
dopplershift authored Dec 1, 2017
2 parents 725d8d6 + 4341f00 commit 0ddae6e
Show file tree
Hide file tree
Showing 5 changed files with 3,432 additions and 784 deletions.
106 changes: 69 additions & 37 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,9 @@ def _update_padding(self, pad, angle):
def update_position(self, loc):
super(ThetaTick, self).update_position(loc)
axes = self.axes
angle = (loc * axes.get_theta_direction() +
axes.get_theta_offset() - np.pi / 2)
angle = loc * axes.get_theta_direction() + axes.get_theta_offset()
text_angle = np.rad2deg(angle) % 360 - 90
angle -= np.pi / 2

if self.tick1On:
marker = self.tick1line.get_marker()
Expand All @@ -354,17 +355,17 @@ def update_position(self, loc):

mode, user_angle = self._labelrotation
if mode == 'default':
angle = 0
text_angle = user_angle
else:
if angle > np.pi / 2:
angle -= np.pi
elif angle < -np.pi / 2:
angle += np.pi
angle = np.rad2deg(angle) + user_angle
if text_angle > 90:
text_angle -= 180
elif text_angle < -90:
text_angle += 180
text_angle += user_angle
if self.label1On:
self.label1.set_rotation(angle)
self.label1.set_rotation(text_angle)
if self.label2On:
self.label2.set_rotation(angle)
self.label2.set_rotation(text_angle)

# This extra padding helps preserve the look from previous releases but
# is also needed because labels are anchored to their center.
Expand Down Expand Up @@ -542,17 +543,59 @@ def _get_text2(self):
t.set_rotation_mode('anchor')
return t

def _determine_anchor(self, angle, start):
if start:
if -90 <= angle <= 90:
return 'left', 'center'
def _determine_anchor(self, mode, angle, start):
# Note: angle is the (spine angle - 90) because it's used for the tick
# & text setup, so all numbers below are -90 from (normed) spine angle.
if mode == 'auto':
if start:
if -90 <= angle <= 90:
return 'left', 'center'
else:
return 'right', 'center'
else:
return 'right', 'center'
if -90 <= angle <= 90:
return 'right', 'center'
else:
return 'left', 'center'
else:
if -90 <= angle <= 90:
return 'right', 'center'
if start:
if angle < -68.5:
return 'center', 'top'
elif angle < -23.5:
return 'left', 'top'
elif angle < 22.5:
return 'left', 'center'
elif angle < 67.5:
return 'left', 'bottom'
elif angle < 112.5:
return 'center', 'bottom'
elif angle < 157.5:
return 'right', 'bottom'
elif angle < 202.5:
return 'right', 'center'
elif angle < 247.5:
return 'right', 'top'
else:
return 'center', 'top'
else:
return 'left', 'center'
if angle < -68.5:
return 'center', 'bottom'
elif angle < -23.5:
return 'right', 'bottom'
elif angle < 22.5:
return 'right', 'center'
elif angle < 67.5:
return 'right', 'top'
elif angle < 112.5:
return 'center', 'top'
elif angle < 157.5:
return 'left', 'top'
elif angle < 202.5:
return 'left', 'center'
elif angle < 247.5:
return 'left', 'bottom'
else:
return 'center', 'bottom'

def update_position(self, loc):
super(RadialTick, self).update_position(loc)
Expand All @@ -565,7 +608,8 @@ def update_position(self, loc):
full = _is_full_circle_deg(thetamin, thetamax)

if full:
angle = axes.get_rlabel_position() * direction + offset - 90
angle = (axes.get_rlabel_position() * direction +
offset) % 360 - 90
tick_angle = 0
if angle > 90:
text_angle = angle - 180
Expand All @@ -574,7 +618,7 @@ def update_position(self, loc):
else:
text_angle = angle
else:
angle = thetamin * direction + offset - 90
angle = (thetamin * direction + offset) % 360 - 90
if direction > 0:
tick_angle = np.deg2rad(angle)
else:
Expand All @@ -595,7 +639,7 @@ def update_position(self, loc):
ha = 'left'
va = 'bottom'
else:
ha, va = self._determine_anchor(angle, True)
ha, va = self._determine_anchor(mode, angle, direction > 0)
self.label1.set_ha(ha)
self.label1.set_va(va)
self.label1.set_rotation(text_angle)
Expand All @@ -622,7 +666,7 @@ def update_position(self, loc):
self.label2On = False
self.tick2On = False
else:
angle = thetamax * direction + offset - 90
angle = (thetamax * direction + offset) % 360 - 90
if direction > 0:
tick_angle = np.deg2rad(angle)
else:
Expand All @@ -639,7 +683,7 @@ def update_position(self, loc):
else:
text_angle = user_angle
if self.label2On:
ha, va = self._determine_anchor(angle, False)
ha, va = self._determine_anchor(mode, angle, direction < 0)
self.label2.set_ha(ha)
self.label2.set_va(va)
self.label2.set_rotation(text_angle)
Expand Down Expand Up @@ -932,16 +976,10 @@ def get_xaxis_transform(self, which='grid'):
return self._xaxis_transform

def get_xaxis_text1_transform(self, pad):
if _is_full_circle_rad(*self._realViewLim.intervalx):
return self._xaxis_text_transform, 'center', 'center'
else:
return self._xaxis_text_transform, 'bottom', 'center'
return self._xaxis_text_transform, 'center', 'center'

def get_xaxis_text2_transform(self, pad):
if _is_full_circle_rad(*self._realViewLim.intervalx):
return self._xaxis_text_transform, 'center', 'center'
else:
return self._xaxis_text_transform, 'top', 'center'
return self._xaxis_text_transform, 'center', 'center'

def get_yaxis_transform(self, which='grid'):
if which in ('tick1', 'tick2'):
Expand Down Expand Up @@ -1116,10 +1154,6 @@ def set_theta_direction(self, direction):
raise ValueError(
"direction must be 1, -1, clockwise or counterclockwise")
self._direction.invalidate()
# FIXME: Why is this needed? Even though the tick label gets
# re-created, the alignment is not correctly updated without a reset.
self.yaxis.reset_ticks()
self.yaxis.set_clip_path(self.patch)

def get_theta_direction(self):
"""
Expand Down Expand Up @@ -1176,8 +1210,6 @@ def set_rlabel_position(self, value):
The angular position of the radius labels in degrees.
"""
self._r_label_position.clear().translate(np.deg2rad(value), 0.0)
self.yaxis.reset_ticks()
self.yaxis.set_clip_path(self.patch)

def set_yscale(self, *args, **kwargs):
Axes.set_yscale(self, *args, **kwargs)
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0ddae6e

Please sign in to comment.