Skip to content

Commit 9356808

Browse files
committed
implement xtick rotation_mode
1 parent 0439b37 commit 9356808

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/matplotlib/axis.py

+6-1
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/tests/test_text.py

+9
Original file line numberDiff line numberDiff line change
@@ -1135,3 +1135,12 @@ def test_font_wrap():
11351135
plt.text(3, 4, t, family='monospace', ha='right', wrap=True)
11361136
plt.text(-1, 0, t, fontsize=14, style='italic', ha='left', rotation=-15,
11371137
wrap=True)
1138+
1139+
1140+
def test_ha_for_angle():
1141+
text_instance = Text()
1142+
angles = np.arange(0, 360.1, 0.1)
1143+
for angle in angles:
1144+
alignment = text_instance.ha_for_angle(angle)
1145+
assert alignment in ['center', 'left', 'right'], \
1146+
f"Invalid alignment at angle {angle:.1f}: {alignment}"

lib/matplotlib/text.py

+16-2
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

@@ -454,6 +454,9 @@ def _get_layout(self, renderer):
454454

455455
rotation_mode = self.get_rotation_mode()
456456
if rotation_mode != "anchor":
457+
if rotation_mode == 'xtick':
458+
angle = self.get_rotation()
459+
halign = self.ha_for_angle(angle)
457460
# compute the text location in display coords and the offsets
458461
# necessary to align the bbox with that location
459462
if halign == 'center':
@@ -1380,6 +1383,18 @@ def set_fontname(self, fontname):
13801383
"""
13811384
self.set_fontfamily(fontname)
13821385

1386+
def ha_for_angle(self, angle):
1387+
"""
1388+
Determines horizontal alignment ('ha') based on the angle of rotation.
1389+
"""
1390+
if (angle < 5 or 85 <= angle < 105 or 355 <= angle <= 360 or
1391+
170 <= angle < 190 or 265 <= angle < 275):
1392+
return 'center'
1393+
elif 5 <= angle < 85 or 190 <= angle < 265:
1394+
return 'right'
1395+
elif 105 <= angle < 170 or 275 <= angle < 355:
1396+
return 'left'
1397+
13831398

13841399
class OffsetFrom:
13851400
"""Callable helper class for working with `Annotation`."""
@@ -2031,5 +2046,4 @@ def get_tightbbox(self, renderer=None):
20312046
return Bbox.null()
20322047
return super().get_tightbbox(renderer)
20332048

2034-
20352049
_docstring.interpd.register(Annotation=Annotation.__init__.__doc__)

0 commit comments

Comments
 (0)