Skip to content

Commit 6737b13

Browse files
authored
Make anchor_mode default (#161)
* make anchor_mode default * add unittest * add some acute angles
1 parent 31f6645 commit 6737b13

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ultraplot/axes/cartesian.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ def _update_rotation(self, s, *, rotation=None):
825825
kw = {"rotation": rotation}
826826
if rotation not in (0, 90, -90):
827827
kw["ha"] = "right" if rotation > 0 else "left"
828+
kw["rotation_mode"] = "anchor"
828829
for label in axis.get_ticklabels():
829830
label.update(kw)
830831
setattr(self, current, rotation)

ultraplot/tests/test_format.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,39 @@ def test_loc_positions():
529529
for loc in TEXT_LOCS:
530530
ax.format(abc="a.", abcloc=loc)
531531
uplt.close(fig)
532+
533+
534+
@pytest.mark.parametrize("angle", [0, 45, 89, 63, 90])
535+
def test_axis_label_anchor(angle):
536+
"""
537+
Check if the rotation of the xticklabels is correctly handle by xrotation and yrotation
538+
"""
539+
fig, ax = uplt.subplots(ncols=2)
540+
ax[0].format(xrotation=angle, yrotation=angle)
541+
542+
# Need fixed ticks for it to work (set locator explicitly)
543+
ax[1].set_xticks(ax[1].get_xticks())
544+
ax[1].set_yticks(ax[1].get_yticks())
545+
546+
kw = dict()
547+
if angle in (0, 90, -90):
548+
kw["ha"] = "right"
549+
ax[1].set_xticklabels(
550+
ax[1].get_xticklabels(), rotation=angle, rotation_mode="anchor", **kw
551+
)
552+
ax[1].set_yticklabels(
553+
ax[1].get_yticklabels(), rotation=angle, rotation_mode="anchor", **kw
554+
)
555+
556+
# Ticks should be in the same position
557+
for tick1, tick2 in zip(ax[0].get_xticklabels(), ax[1].get_xticklabels()):
558+
assert tick1.get_rotation() == angle
559+
assert tick2.get_rotation() == angle
560+
assert tick1.get_position()[0] == tick2.get_position()[0]
561+
assert tick1.get_position()[1] == tick2.get_position()[1]
562+
563+
for tick1, tick2 in zip(ax[0].get_yticklabels(), ax[1].get_yticklabels()):
564+
assert tick1.get_rotation() == angle
565+
assert tick2.get_rotation() == angle
566+
assert tick1.get_position()[0] == tick2.get_position()[0]
567+
assert tick1.get_position()[1] == tick2.get_position()[1]

0 commit comments

Comments
 (0)