Skip to content

ValueError exception added to handle mix of {} and % string in colorbar format #27015

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

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
try:
self._formatter = ticker.FormatStrFormatter(format)
_ = self._formatter(0)
except TypeError:
except (TypeError, ValueError):
self._formatter = ticker.StrMethodFormatter(format)
else:
self._formatter = format # Assume it is a Formatter or None
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
BoundaryNorm, LogNorm, PowerNorm, Normalize, NoNorm
)
from matplotlib.colorbar import Colorbar
from matplotlib.ticker import FixedLocator, LogFormatter
from matplotlib.ticker import FixedLocator, LogFormatter, StrMethodFormatter
from matplotlib.testing.decorators import check_figures_equal


Expand Down Expand Up @@ -1230,3 +1230,9 @@ def test_colorbar_wrong_figure():
fig_tl.colorbar(im)
fig_tl.draw_without_rendering()
fig_cl.draw_without_rendering()


def test_colorbar_format_string_and_old():
plt.imshow([[0, 1]])
cb = plt.colorbar(format="{x}%")
assert isinstance(cb._formatter, StrMethodFormatter)