10000 Polar tick improvements by QuLogic · Pull Request #9068 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Polar tick improvements #9068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add an 'auto' option to label rotation.
This option enables the automatic rotation of polar tick labels.
  • Loading branch information
QuLogic committed Sep 25, 2017
commit 228ee536f81ac92d69caef1a0b40669a5ea96ad1
11 changes: 7 additions & 4 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ negative values are simply used as labels, and the real radius is shifted by
the configured minimum. This release also allows negative radii to be used for
grids and ticks, which were previously silently ignored.

For plots of a partial circle, radial ticks and tick labels have been modified
to be parallel to the circular grid line. Angular ticks have been modified to
be parallel to the grid line and the labels are now perpendicular to the grid
line (i.e., parallel to the outer boundary.)
Radial ticks have be modified to be parallel to the circular grid line. Angular
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, small grammar typo here.

ticks will be modified to be parallel to the grid line. It may also be useful
to rotate tick labels to match the boundary. Calling
``ax.tick_params(rotation='auto')`` will enable this new behavior. Radial tick
labels will be modified to be parallel to the circular grid line. Angular tick
labels will be perpendicular to the grid line (i.e., parallel to the outer
boundary.)


Merge JSAnimation
Expand Down
24 changes: 22 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, axes, loc, label,
labelsize = rcParams['%s.labelsize' % name]
self._labelsize = labelsize

self._labelrotation = labelrotation
self._set_labelrotation(labelrotation)

if zorder is None:
if major:
Expand All @@ -167,6 +167,20 @@ def __init__(self, axes, loc, label,

self.update_position(loc)

def _set_labelrotation(self, labelrotation):
if isinstance(labelrotation, six.string_types):
mode = labelrotation
angle = 0
elif isinstance(labelrotation, (tuple, list)):
mode, angle = labelrotation
else:
mode = 'default'
angle = labelrotation
if mode not in ('auto', 'default'):
raise ValueError("Label rotation mode must be 'default' or "
"'auto', not '{}'.".format(mode))
self._labelrotation = (mode, angle)

def apply_tickdir(self, tickdir):
"""
Calculate self._pad and self._tickmarkers
Expand Down Expand Up @@ -331,8 +345,14 @@ def _apply_params(self, **kw):
self.tick2line.set(**tick_kw)
for k, v in six.iteritems(tick_kw):
setattr(self, '_' + k, v)

if 'labelrotation' in kw:
self._set_labelrotation(kw.pop('labelrotation'))
self.label1.set(rotation=self._labelrotation[1])
self.label2.set(rotation=self._labelrotation[1])

label_list = [k for k in six.iteritems(kw)
if k[0] in ['labelsize', 'labelcolor', 'labelrotation']]
if k[0] in ['labelsize', 'labelcolor']]
if label_list:
label_kw = {k[5:]: v for k, v in label_list}
self.label1.set(**label_kw)
Expand Down
42 changes: 29 additions & 13 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import six

from collections import OrderedDict

import numpy as np
Expand Down Expand Up @@ -251,9 +253,9 @@ class ThetaTick(maxis.XTick):
tick location. This results in ticks that are correctly perpendicular to
the arc spine.

Labels are also rotated to be parallel to the spine. The label padding is
also applied here since it's not possible to use a generic axes transform
to produce tick-specific padding.
When 'auto' rotation is enabled, labels are also rotated to be parallel to
the spine. The label padding is also applied here since it's not possible
to use a generic axes transform to produce tick-specific padding.
"""
def __init__(self, axes, *args, **kwargs):
self._text1_translate = mtransforms.ScaledTranslation(
Expand Down Expand Up @@ -322,14 +324,15 @@ def update_position(self, loc):
trans = self.tick2line._marker._transform
self.tick2line._marker._transform = trans

if not _is_full_circle_deg(axes.get_thetamin(), axes.get_thetamax()):
mode, user_angle = self._labelrotation
if mode == 'default':
angle = 0
else:
if angle > np.pi / 2:
angle -= np.pi
elif angle < -np.pi / 2:
angle += np.pi
else:
angle = 0
angle = np.rad2deg(angle) + self._labelrotation
angle = np.rad2deg(angle) + user_angle
if self.label1On:
self.label1.set_rotation(angle)
if self.label2On:
Expand Down Expand Up @@ -488,8 +491,8 @@ class RadialTick(maxis.YTick):
This subclass of `YTick` provides radial ticks with some small modification
to their re-positioning such that ticks are rotated based on axes limits.
This results in ticks that are correctly perpendicular to the spine. Labels
are also rotated to be perpendicular to the spine, but only for wedges, to
preserve backwards compatibility.
are also rotated to be perpendicular to the spine, when 'auto' rotation is
enabled.
"""
def _get_text1(self):
t = super(RadialTick, self)._get_text1()
Expand Down Expand Up @@ -524,9 +527,14 @@ def update_position(self, loc):
full = _is_full_circle_deg(thetamin, thetamax)

if full:
angle = axes.get_rlabel_position()
angle = axes.get_rlabel_position() * direction + offset - 90
tick_angle = 0
text_angle = 0
if angle > 90:
text_angle = angle - 180
elif angle < -90:
text_angle = angle + 180
else:
text_angle = angle
else:
angle = thetamin * direction + offset - 90
if direction > 0:
Expand All @@ -539,7 +547,11 @@ def update_position(self, loc):
text_angle = angle + 180
else:
text_angle = angle
text_angle += self._labelrotation
mode, user_angle = self._labelrotation
if mode == 'auto':
text_angle += user_angle
else:
text_angle = user_angle
if self.label1On:
if full:
ha = 'left'
Expand Down Expand Up @@ -583,7 +595,11 @@ def update_position(self, loc):
text_angle = angle + 180
else:
text_angle = angle
text_angle += self._labelrotation
mode, user_angle = self._labelrotation
if mode == 'auto':
text_angle += user_angle
else:
text_angle = user_angle
if self.label2On:
ha, va = self._determine_anchor(angle, False)
self.label2.set_ha(ha)
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def test_polar_rlabel_position():
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.set_rlabel_position(315)
ax.tick_params(rotation='auto')


@image_comparison(baseline_images=['polar_theta_wedge'], style='default',
Expand All @@ -692,8 +693,9 @@ def test_polar_theta_limits():
ax.set_thetamin(start)
ax.set_thetamax(end)
ax.tick_params(tick1On=True, tick2On=True,
direction=DIRECTIONS[i % len(DIRECTIONS)])
ax.yaxis.set_tick_params(label2On=True)
direction=DIRECTIONS[i % len(DIRECTIONS)],
rotation='auto')
ax.yaxis.set_tick_params(label2On=True, rotation='auto')
else:
ax.set_visible(False)

Expand Down
0