Skip to content

Don't require an axis in PercentFormatter with explicit decimals#31995

Open
Sreekant13 wants to merge 1 commit into
matplotlib:mainfrom
Sreekant13:fix/percentformatter-explicit-decimals-no-axis
Open

Don't require an axis in PercentFormatter with explicit decimals#31995
Sreekant13 wants to merge 1 commit into
matplotlib:mainfrom
Sreekant13:fix/percentformatter-explicit-decimals-no-axis

Conversation

@Sreekant13

Copy link
Copy Markdown

PR summary

PercentFormatter.__call__ always reads self.axis.get_view_interval(), but that display range is only used to choose the number of decimals automatically, i.e. when decimals is None. With decimals set explicitly, calling the formatter while it is not attached to an axis raised AttributeError, even though the axis value is never needed.

  • What problem does it solve? A PercentFormatter created with explicit decimals already has everything it needs to format a value, but it still required an axis, so using it standalone (for example to build a label or annotation) crashed.
  • Reasoning for the implementation: the axis view interval is only consulted in the auto-decimals branch of format_pct, so __call__ now reads it only when decimals is None. Auto-decimals still requires an axis, unchanged.

Minimal self-contained example:

import matplotlib.ticker as mticker

# Before: AttributeError: 'NoneType' object has no attribute 'get_view_interval'
# After:  '50.0%'
print(mticker.PercentFormatter(xmax=1.0, decimals=1)(0.5))

PR checklist

  • [N/A] "closes #0000" is in the body of the PR description - no issue; small self-contained bug fix (happy to open one if preferred)
  • new and changed code is tested - added TestPercentFormatter.test_call_without_axis; existing format_pct cases are unchanged and still pass
  • [N/A] Plotting related features are demonstrated in an example - bug fix, no new plotting feature
  • [N/A] New Features and API Changes are noted with a directive and release note - bug fix, not a new feature or API change
  • Documentation complies with general and docstring guidelines - no documentation changes; existing docstrings unaffected

PercentFormatter.__call__ always read self.axis.get_view_interval(), but that
display range is only used to choose the number of decimals automatically,
i.e. when decimals is None. With decimals set explicitly, calling the
formatter while it was not attached to an axis raised AttributeError even
though the axis value was never needed. Only read the axis in the auto-decimals
case, and add a regression test.
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@dstansby dstansby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable improvement - thanks for the PR

@dstansby dstansby added this to the v3.12.0 milestone Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants