Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4937314

Browse files
authored
Merge pull request #9452 from afvincent/fix_incorrect_labels_issue_9397
FIX: Always update tick labels (fixes #9397)
2 parents 7cca455 + 27fd53a commit 4937314

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/matplotlib/axis.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,11 +1052,12 @@ def _update_ticks(self, renderer):
10521052
for tick, loc, label in tick_tups:
10531053
if tick is None:
10541054
continue
1055-
if not mtransforms.interval_contains(interval_expanded, loc):
1056-
continue
1055+
# NB: always update labels and position to avoid issues like #9397
10571056
tick.update_position(loc)
10581057
tick.set_label1(label)
10591058
tick.set_label2(label)
1059+
if not mtransforms.interval_contains(interval_expanded, loc):
1060+
continue
10601061
ticks_to_draw.append(tick)
10611062

10621063
return ticks_to_draw

lib/matplotlib/tests/test_axes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4570,6 +4570,25 @@ def test_set_get_ticklabels():
45704570
ax[1].set_yticklabels(ax[0].get_yticklabels())
45714571

45724572

4573+
def test_tick_label_update():
4574+
# test issue 9397
4575+
4576+
fig, ax = plt.subplots()
4577+
4578+
# Set up a dummy formatter
4579+
def formatter_func(x, pos):
4580+
return "unit value" if x == 1 else ""
4581+
ax.xaxis.set_major_formatter(plt.FuncFormatter(formatter_func))
4582+
4583+
# Force some of the x-axis ticks to be outside of the drawn range
4584+
ax.set_xticks([-1, 0, 1, 2, 3])
4585+
ax.set_xlim(-0.5, 2.5)
4586+
4587+
ax.figure.canvas.draw()
4588+
tick_texts = [tick.get_text() for tick in ax.xaxis.get_ticklabels()]
4589+
assert tick_texts == ["", "", "unit value", "", ""]
4590+
4591+
45734592
@image_comparison(baseline_images=['o_marker_path_snap'], extensions=['png'],
45744593
savefig_kwarg={'dpi': 72})
45754594
def test_o_marker_path_snap():

0 commit comments

Comments
 (0)