Skip to content

Commit ee92312

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

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-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: 18 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,24 @@ 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 80 <= angle < 110 or 340 <= angle <= 360 or
462+
160 <= angle < 200 or 240 <= angle < 300):
463+
return 'center'
464+
elif 20 <= angle < 80 or 200 <= angle < 240:
465+
return 'right'
466+
elif 110 <= angle < 160 or 300 <= angle < 340:
467+
return 'left'
468+
456469
if rotation_mode != "anchor":
470+
if rotation_mode == 'xtick':
471+
# Compute horizontal alignment based on the angle
472+
angle = self.get_rotation()
473+
halign = ha_for_angle(angle) # Use Tim's function
457474
# compute the text location in display coords and the offsets
458475
# necessary to align the bbox with that location
459476
if halign == 'center':

0 commit comments

Comments
 (0)