Skip to content

Commit 92c0a43

Browse files
committed
implement xtick rotation mode
1 parent 0439b37 commit 92c0a43

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/matplotlib/axis.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ def _apply_params(self, **kwargs):
346346
if k in _gridline_param_names}
347347
self.gridline.set(**grid_kw)
348348

349+
if 'rotation_mode' in kwargs:
350+
rotation_mode = kwargs.pop('rotation_mode')
351+
self.label1.set_rotation_mode(rotation_mode)
352+
self.label2.set_rotation_mode(rotation_mode)
353+
349354
def update_position(self, loc):
350355
"""Set the location of tick in data coords with scalar *loc*."""
351356
raise NotImplementedError('Derived must override')
@@ -1072,7 +1077,7 @@ def _translate_tick_params(kw, reverse=False):
10721077
'tick1On', 'tick2On', 'label1On', 'label2On',
10731078
'length', 'direction', 'left', 'bottom', 'right', 'top',
10741079
'labelleft', 'labelbottom', 'labelright', 'labeltop',
1075-
'labelrotation',
1080+
'labelrotation', 'rotation_mode',
10761081
*_gridline_param_names]
10771082

10781083
keymap = {

lib/matplotlib/text.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def set_rotation_mode(self, m):
310310
if m is None:
311311
m = "default"
312312
else:
313-
_api.check_in_list(("anchor", "default"), rotation_mode=m)
313+
_api.check_in_list(("anchor", "default", "xtick"), rotation_mode=m)
314314
self._rotation_mode = m
315315
self.stale = True
316316

@@ -453,7 +453,25 @@ def _get_layout(self, renderer):
453453
valign = self._verticalalignment
454454

455455
rotation_mode = self.get_rotation_mode()
456+
457+
def ha_for_angle(angle):
458+
"""
459+
Determines horizontal alignment ('ha') based on the angle of rotation.
460+
"""
461+
if angle < 20 or 340 <= angle <= 360 or 160 <= angle < 200:
462+
return 'center'
463+
elif 20 <= angle < 60 or 200 <= angle < 240 or 300 <= angle < 340:
464+
return 'right'
465+
elif 60 <= angle < 120 or 240 <= angle < 300:
466+
return 'center'
467+
elif 120 <= angle < 160:
468+
return 'left'
469+
456470
if rotation_mode != "anchor":
471+
if rotation_mode == 'xtick':
472+
# Compute horizontal alignment based on the angle
473+
angle = self.get_rotation()
474+
halign = ha_for_angle(angle) # Use Tim's function
457475
# compute the text location in display coords and the offsets
458476
# necessary to align the bbox with that location
459477
if halign == 'center':

0 commit comments

Comments
 (0)