Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7252158

Browse filesBrowse files
authored
Merge pull request #9743 from jklymak/fixclabel
FIX: check if contour level in format dictionary, or return a default
2 parents f84da20 + 7586225 commit 7252158
Copy full SHA for 7252158

File tree

Expand file treeCollapse file tree

2 files changed

+20
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+20
-1
lines changed

‎lib/matplotlib/contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/contour.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def get_text(self, lev, fmt):
328328
return lev
329329
else:
330330
if isinstance(fmt, dict):
331-
return fmt[lev]
331+
return fmt.get(lev, '%1.3f')
332332
elif callable(fmt):
333333
return fmt(lev)
334334
else:

‎lib/matplotlib/tests/test_contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_contour.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,25 @@ def test_contour_empty_levels():
143143
assert len(record) == 1
144144

145145

146+
def test_contour_badlevel_fmt():
147+
# test funny edge case from
148+
# https://github.com/matplotlib/matplotlib/issues/9742
149+
# User supplied fmt for each level as a dictionary, but
150+
# MPL changed the level to the minimum data value because
151+
# no contours possible.
152+
# This would error out pre
153+
# https://github.com/matplotlib/matplotlib/pull/9743
154+
x = np.arange(9)
155+
z = np.zeros((9, 9))
156+
157+
fig, ax = plt.subplots()
158+
fmt = {1.: '%1.2f'}
159+
with pytest.warns(UserWarning) as record:
160+
cs = ax.contour(x, x, z, levels=[1.])
161+
ax.clabel(cs, fmt=fmt)
162+
assert len(record) == 1
163+
164+
146165
def test_contour_uniform_z():
147166

148167
x = np.arange(9)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.