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

Skip to content

Commit 7f63a18

Browse files
authored
Merge pull request #27015 from borgesaugusto/iss_27007
ValueError exception added to handle mix of {} and % string in colorbar format
2 parents cfe5bf7 + 10b4157 commit 7f63a18

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
404404
try:
405405
self._formatter = ticker.FormatStrFormatter(format)
406406
_ = self._formatter(0)
407-
except TypeError:
407+
except (TypeError, ValueError):
408408
self._formatter = ticker.StrMethodFormatter(format)
409409
else:
410410
self._formatter = format # Assume it is a Formatter or None

lib/matplotlib/tests/test_colorbar.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
BoundaryNorm, LogNorm, PowerNorm, Normalize, NoNorm
1616
)
1717
from matplotlib.colorbar import Colorbar
18-
from matplotlib.ticker import FixedLocator, LogFormatter
18+
from matplotlib.ticker import FixedLocator, LogFormatter, StrMethodFormatter
1919
from matplotlib.testing.decorators import check_figures_equal
2020

2121

@@ -1230,3 +1230,9 @@ def test_colorbar_wrong_figure():
12301230
fig_tl.colorbar(im)
12311231
fig_tl.draw_without_rendering()
12321232
fig_cl.draw_without_rendering()
1233+
1234+
1235+
def test_colorbar_format_string_and_old():
1236+
plt.imshow([[0, 1]])
1237+
cb = plt.colorbar(format="{x}%")
1238+
assert isinstance(cb._formatter, StrMethodFormatter)

0 commit comments

Comments
 (0)