Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bef46ab

Browse filesBrowse files
committed
Add a custom ThetaTick for polar plots.
This is able to add a position-dependent rotation to the tick marker, ensuring it is correctly perpendicular to the spine. It also rotates the tick label so that it is parallel to the spine.
1 parent ce445cb commit bef46ab
Copy full SHA for bef46ab

File tree

Expand file treeCollapse file tree

1 file changed

+56
-3
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+56
-3
lines changed

‎lib/matplotlib/projections/polar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/projections/polar.py
+56-3Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import matplotlib.axis as maxis
1010
from matplotlib import cbook
1111
from matplotlib import docstring
12+
import matplotlib.markers as mmarkers
1213
import matplotlib.patches as mpatches
1314
import matplotlib.path as mpath
1415
from matplotlib import rcParams
@@ -240,6 +241,58 @@ def zoom(self, direction):
240241
return self.base.zoom(direction)
241242

242243

244+
class ThetaTick(maxis.XTick):
245+
"""
246+
A theta-axis tick.
247+
248+
This subclass of `XTick` provides angular ticks with some small
249+
modification to their re-positioning such that ticks are rotated based on
250+
tick location. This results in ticks that are correctly perpendicular to
251+
the arc spine. Labels are also rotated to be parallel to the spine.
252+
"""
253+
def _get_text1(self):
254+
t = maxis.XTick._get_text1(self)
255+
t.set_rotation_mode('anchor')
256+
return t
257+
258+
def _get_text2(self):
259+
t = maxis.XTick._get_text2(self)
260+
t.set_rotation_mode('anchor')
261+
return t
262+
263+
def update_position(self, loc):
264+
maxis.XTick.update_position(self, loc)
265+
axes = self.axes
266+
angle = (loc * axes.get_theta_direction() +
267+
axes.get_theta_offset() - np.pi / 2)
268+
269+
if self.tick1On:
270+
marker = self.tick1line.get_marker()
271+
if marker in (mmarkers.TICKUP, '|'):
272+
trans = mtransforms.Affine2D().scale(1.0, 1.0).rotate(angle)
273+
elif marker == mmarkers.TICKDOWN:
274+
trans = mtransforms.Affine2D().scale(1.0, -1.0).rotate(angle)
275+
else:
276+
# Don't modify custom tick line markers.
277+
trans = self.tick1line._marker._transform
278+
self.tick1line._marker._transform = trans
279+
if self.tick2On:
280+
marker = self.tick2line.get_marker()
281+
if marker in (mmarkers.TICKUP, '|'):
282+
trans = mtransforms.Affine2D().scale(1.0, 1.0).rotate(angle)
283+
elif marker == mmarkers.TICKDOWN:
284+
trans = mtransforms.Affine2D().scale(1.0, -1.0).rotate(angle)
285+
else:
286+
# Don't modify custom tick line markers.
287+
trans = self.tick2line._marker._transform
288+
self.tick2line._marker._transform = trans
289+
290+
if self.label1On:
291+
self.label1.set_rotation(np.rad2deg(angle) + self._labelrotation)
292+
if self.label2On:
293+
self.label2.set_rotation(np.rad2deg(angle) + self._labelrotation)
294+
295+
243296
class ThetaAxis(maxis.XAxis):
244297
"""
245298
A theta Axis.
@@ -255,7 +308,7 @@ def _get_tick(self, major):
255308
tick_kw = self._major_tick_kw
256309
else:
257310
tick_kw = self._minor_tick_kw
258-
return maxis.XTick(self.axes, 0, '', major=major, **tick_kw)
311+
return ThetaTick(self.axes, 0, '', major=major, **tick_kw)
259312

260313
def _wrap_locator_formatter(self):
261314
self.set_major_locator(ThetaLocator(self.get_major_locator()))
@@ -587,10 +640,10 @@ def get_xaxis_transform(self, which='grid'):
587640
return self._xaxis_transform
588641

589642
def get_xaxis_text1_transform(self, pad):
590-
return self._xaxis_text1_transform, 'center', 'center'
643+
return self._xaxis_text1_transform, 'bottom', 'center'
591644

592645
def get_xaxis_text2_transform(self, pad):
593-
return self._xaxis_text2_transform, 'center', 'center'
646+
return self._xaxis_text2_transform, 'top', 'center'
594647

595648
def get_yaxis_transform(self, which='grid'):
596649
if which in ('tick1', 'tick2'):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.