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

Skip to content
Open
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
13 changes: 11 additions & 2 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import functools
import math
from numbers import Integral

import warnings
import numpy as np
from numpy import ma

Expand Down Expand Up @@ -998,15 +998,24 @@
one contour line, but two filled regions, and therefore
three levels to provide boundaries for both regions.
"""


if self.zmin == self.zmax:
warnings.warn(
"Z is constant; contour levels are not meaningful.",
stacklevel=2
)
return np.array([self.zmin])

lev = self.locator.tick_values(self.zmin, self.zmax)

try:
if self.locator._symmetric:

Check warning on line 1013 in lib/matplotlib/contour.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/contour.py" range:{start:{line:1013 column:1} end:{line:1013 column:8}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:1013 column:1} end:{line:1013 column:8}}}
return lev
except AttributeError:
pass

# Trim excess levels the locator may have supplied.
# Trim excess levels the locator may have supplied.
under = np.nonzero(lev < self.zmin)[0]
i0 = under[-1] if len(under) else 0
over = np.nonzero(lev > self.zmax)[0]
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,16 @@ def test_contour_aliases(fig_test, fig_ref):
def test_contour_singular_color():
with pytest.raises(TypeError):
plt.figure().add_subplot().contour([[0, 1], [2, 3]], color="r")

def test_contour_constant_z_warns_and_single_level():
Z = np.ones((10, 10))

fig, ax = plt.subplots()

# Expect a warning
with pytest.warns(UserWarning, match="Z is constant"):
cs = ax.contour(Z)

# Ensure only one level is created
assert len(cs.levels) == 1
assert cs.levels[0] == 1.0
Loading