Pin tick locations before setting rotated tick labels - #289
Open
DMZ22 wants to merge 1 commit into
Open
Conversation
AxisTickLabelsDecorator.decorate() called set_ticklabels() in the rotation branch without first calling set_ticks(), which installs a FixedFormatter over the default non-fixed locator. The elif branch below it already gets this right. The result is worse than the UserWarning suggests: matplotlib keeps its auto-generated tick positions and attaches the labels to the first few of them, so on a three-label chart "alpha" lands at x=-0.25 and eight blank ticks follow. Setting the ticks first puts the labels back on the positions they describe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #157.
In
AxisTickLabelsDecorator.decorate()theif self._rotation:branch callsset_ticklabels()with no precedingset_ticks(), while theelif self._tick_values is None:branch immediately below it does call it. Setting labels installs aFixedFormatter, and pairing that with the default non-fixed locator is what matplotlib warns about.The warning is understating it — the labels end up on the wrong positions. With three labels on an axis using the default locator:
matplotlib keeps its eleven auto-generated tick positions and attaches the three labels to the first three of them, so
alpharenders at x=-0.25 rather than x=0 and eight blank ticks trail after it. That is a mislabelled axis, not just a noisy log line.The fix mirrors what the branch below already does — set the ticks first when no explicit
tick_valueswere supplied.Both affected call paths are live:
create_return_quantiles.py:65constructs the decorator with a rotation and no tick values (that isdemo_scripts/charts/return_quantiles_chart.py, the reproduction named in the issue), andheatmap_demo.pyreaches it viarotation='auto'.Tests. There was no plotting test package under
qf_lib/tests/unit_tests/, so I added one withtest_axis_tick_labels_decorator.py. It covers the two rotation paths, the non-rotated path, the explicit-tick_valuespath, and an assertion that the rotated labels are still applied to the right ticks at the right angle.Reverting only the decorator while keeping the tests fails three of the five — including the "labels are still applied" one, which is the correctness half. The other two pass either way by design, since their job is to confirm the already-correct branches are untouched.
Verified on Python 3.11 with the versions
requirements.txtpins (numpy 1.26.4, matplotlib 3.6.1).flake8is clean on both files.