Skip to content

[Bug]: Colorbar format string kind guess could be made more robust #27007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anntzer opened this issue Oct 5, 2023 · 1 comment · Fixed by #27015
Closed

[Bug]: Colorbar format string kind guess could be made more robust #27007

anntzer opened this issue Oct 5, 2023 · 1 comment · Fixed by #27015
Labels
API: argument checking Good first issue Open a pull request against these issues if there are no active ones! topic: color/colorbar topic: ticks axis labels
Milestone

Comments

@anntzer
Copy link
Contributor

anntzer commented Oct 5, 2023

Bug summary

When the format kwarg passed to colorbar() is a str, colorbar() currently tries to guess whether it is a %-format or a {}-format string, using

        if isinstance(format, str):
            # Check format between FormatStrFormatter and StrMethodFormatter
            try:
                self._formatter = ticker.FormatStrFormatter(format)
                _ = self._formatter(0)
            except TypeError:
                self._formatter = ticker.StrMethodFormatter(format)

Even ignoring the (contrieved) case where the format string is valid both as a %-format and a {}-format, there are other failure cases which could be easily handled, specifically format="{x}%" ("I want to format the value with a percent-sign appended to it"): here the _formatter(0) call will fail with a ValueError (which just needs to be caught) rather than a TypeError.

Code for reproduction

# see above

Actual outcome

ValueError raised.

Expected outcome

Format string used as expected.

Additional information

Probably just needs additionally catching ValueError, plus a test.

Operating system

No response

Matplotlib Version

3.8

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

from source (.tar.gz)

@anntzer anntzer added Good first issue Open a pull request against these issues if there are no active ones! API: argument checking topic: ticks axis labels topic: color/colorbar labels Oct 5, 2023
@github-actions
Copy link

github-actions bot commented Oct 5, 2023

Good first issue - notes for new contributors

This issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide.

We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR.

If something is unclear, please reach out on any of our communication channels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: argument checking Good first issue Open a pull request against these issues if there are no active ones! topic: color/colorbar topic: ticks axis labels
Projects
None yet
2 participants