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

Skip to content

Commit e162e88

Browse files
committed
Contour levels must be increasing
1 parent 8236ed4 commit e162e88

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2015-11-16 Levels passed to contour(f) and tricontour(f) must be in increasing
2+
order.
3+
14
2015-10-21 Added get_ticks_direction()
25

36
2015-02-27 Added the rcParam 'image.composite_image' to permit users

lib/matplotlib/contour.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,8 @@ def _contour_level_args(self, z, args):
11911191
self.levels = lev
11921192
if self.filled and len(self.levels) < 2:
11931193
raise ValueError("Filled contours require at least 2 levels.")
1194+
if len(self.levels) > 1 and np.amin(np.diff(self.levels)) <= 0.0:
1195+
raise ValueError("Contour levels must be increasing")
11941196

11951197
def _process_levels(self):
11961198
"""
@@ -1674,13 +1676,15 @@ def _initialize_x_y(self, z):
16741676
contour(Z,V)
16751677
contour(X,Y,Z,V)
16761678
1677-
draw contour lines at the values specified in sequence *V*
1679+
draw contour lines at the values specified in sequence *V*,
1680+
which must be in increasing order.
16781681
16791682
::
16801683
16811684
contourf(..., V)
16821685
1683-
fill the ``len(V)-1`` regions between the values in *V*
1686+
fill the ``len(V)-1`` regions between the values in *V*,
1687+
which must be in increasing order.
16841688
16851689
::
16861690
@@ -1743,8 +1747,8 @@ def _initialize_x_y(self, z):
17431747
17441748
*levels*: [level0, level1, ..., leveln]
17451749
A list of floating point numbers indicating the level
1746-
curves to draw; e.g., to draw just the zero contour pass
1747-
``levels=[0]``
1750+
curves to draw, in increasing order; e.g., to draw just
1751+
the zero contour pass ``levels=[0]``
17481752
17491753
*origin*: [ *None* | 'upper' | 'lower' | 'image' ]
17501754
If *None*, the first value of *Z* will correspond to the

lib/matplotlib/tests/test_contour.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from matplotlib import mlab
1010
from matplotlib.testing.decorators import cleanup, image_comparison
1111
from matplotlib import pyplot as plt
12+
from nose.tools import assert_raises
1213

1314
import re
1415

@@ -264,6 +265,14 @@ def test_corner_mask():
264265
plt.contourf(z, corner_mask=corner_mask)
265266

266267

268+
@cleanup
269+
def test_contourf_decreasing_levels():
270+
# github issue 5477.
271+
z = [[0.1, 0.3], [0.5, 0.7]]
272+
plt.figure()
273+
assert_raises(ValueError, plt.contourf, z, [1.0, 0.0])
274+
275+
267276
if __name__ == '__main__':
268277
import nose
269278
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tests/test_triangulation.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from numpy.testing import assert_array_equal, assert_array_almost_equal,\
1111
assert_array_less
1212
import numpy.ma.testutils as matest
13-
from matplotlib.testing.decorators import image_comparison
13+
from matplotlib.testing.decorators import cleanup, image_comparison
1414
import matplotlib.cm as cm
1515
from matplotlib.path import Path
1616

@@ -1021,6 +1021,16 @@ def test_trianalyzer_mismatched_indices():
10211021
triang2 = analyser._get_compressed_triangulation()
10221022

10231023

1024+
@cleanup
1025+
def test_tricontourf_decreasing_levels():
1026+
# github issue 5477.
1027+
x = [0.0, 1.0, 1.0]
1028+
y = [0.0, 0.0, 1.0]
1029+
z = [0.2, 0.4, 0.6]
1030+
plt.figure()
1031+
assert_raises(ValueError, plt.tricontourf, x, y, z, [1.0, 0.0])
1032+
1033+
10241034
if __name__ == '__main__':
10251035
import nose
10261036
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tri/_tri_wrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ static PyObject* PyTriContourGenerator_create_filled_contour(PyTriContourGenerat
295295
return NULL;
296296
}
297297

298+
if (lower_level >= upper_level)
299+
{
300+
PyErr_SetString(PyExc_ValueError,
301+
"filled contour levels must be increasing");
302+
}
303+
298304
PyObject* result;
299305
CALL_CPP("create_filled_contour",
300306
(result = self->ptr->create_filled_contour(lower_level,

lib/matplotlib/tri/tricontour.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ def _contour_args(self, args, kwargs):
141141
142142
tricontour(..., Z, V)
143143
144-
draw contour lines at the values specified in sequence *V*
144+
draw contour lines at the values specified in sequence *V*,
145+
which must be in increasing order.
145146
146147
::
147148
148149
tricontourf(..., Z, V)
149150
150-
fill the (len(*V*)-1) regions between the values in *V*
151+
fill the (len(*V*)-1) regions between the values in *V*,
152+
which must be in increasing order.
151153
152154
::
153155
@@ -186,8 +188,8 @@ def _contour_args(self, args, kwargs):
186188
187189
*levels* [level0, level1, ..., leveln]
188190
A list of floating point numbers indicating the level
189-
curves to draw; e.g., to draw just the zero contour pass
190-
``levels=[0]``
191+
curves to draw, in increasing order; e.g., to draw just
192+
the zero contour pass ``levels=[0]``
191193
192194
*origin*: [ *None* | 'upper' | 'lower' | 'image' ]
193195
If *None*, the first value of *Z* will correspond to the

src/_contour_wrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ static PyObject* PyQuadContourGenerator_create_filled_contour(PyQuadContourGener
9797
return NULL;
9898
}
9999

100+
if (lower_level >= upper_level)
101+
{
102+
PyErr_SetString(PyExc_ValueError,
103+
"filled contour levels must be increasing");
104+
}
105+
100106
PyObject* result;
101107
CALL_CPP("create_filled_contour",
102108
(result = self->ptr->create_filled_contour(lower_level,

0 commit comments

Comments
 (0)