@@ -46,10 +46,9 @@ def test_contour_shape_mismatch_1():
4646 fig = plt .figure ()
4747 ax = fig .add_subplot (111 )
4848
49- try :
49+ with pytest . raises ( TypeError ) as excinfo :
5050 ax .contour (x , y , z )
51- except TypeError as exc :
52- assert exc .args [0 ] == 'Length of x must be number of columns in z.'
51+ excinfo .match (r'Length of x must be number of columns in z.' )
5352
5453
5554def test_contour_shape_mismatch_2 ():
@@ -61,10 +60,9 @@ def test_contour_shape_mismatch_2():
6160 fig = plt .figure ()
6261 ax = fig .add_subplot (111 )
6362
64- try :
63+ with pytest . raises ( TypeError ) as excinfo :
6564 ax .contour (x , y , z )
66- except TypeError as exc :
67- assert exc .args [0 ] == 'Length of y must be number of rows in z.'
65+ excinfo .match (r'Length of y must be number of rows in z.' )
6866
6967
7068def test_contour_shape_mismatch_3 ():
@@ -77,15 +75,13 @@ def test_contour_shape_mismatch_3():
7775 fig = plt .figure ()
7876 ax = fig .add_subplot (111 )
7977
80- try :
78+ with pytest . raises ( TypeError ) as excinfo :
8179 ax .contour (xg , y , z )
82- except TypeError as exc :
83- assert exc .args [0 ] == 'Number of dimensions of x and y should match.'
80+ excinfo .match (r'Number of dimensions of x and y should match.' )
8481
85- try :
82+ with pytest . raises ( TypeError ) as excinfo :
8683 ax .contour (x , yg , z )
87- except TypeError as exc :
88- assert exc .args [0 ] == 'Number of dimensions of x and y should match.'
84+ excinfo .match (r'Number of dimensions of x and y should match.' )
8985
9086
9187def test_contour_shape_mismatch_4 ():
@@ -97,21 +93,15 @@ def test_contour_shape_mismatch_4():
9793 fig = plt .figure ()
9894 ax = fig .add_subplot (111 )
9995
100- try :
96+ with pytest . raises ( TypeError ) as excinfo :
10197 ax .contour (b , g , z )
102- except TypeError as exc :
103- assert re .match (
104- r'Shape of x does not match that of z: ' +
105- r'found \(9L?, 9L?\) instead of \(9L?, 10L?\)\.' ,
106- exc .args [0 ]) is not None , exc .args [0 ]
98+ excinfo .match (r'Shape of x does not match that of z: found \(9L?, 9L?\) ' +
99+ r'instead of \(9L?, 10L?\)' )
107100
108- try :
101+ with pytest . raises ( TypeError ) as excinfo :
109102 ax .contour (g , b , z )
110- except TypeError as exc :
111- assert re .match (
112- r'Shape of y does not match that of z: ' +
113- r'found \(9L?, 9L?\) instead of \(9L?, 10L?\)\.' ,
114- exc .args [0 ]) is not None , exc .args [0 ]
103+ excinfo .match (r'Shape of y does not match that of z: found \(9L?, 9L?\) ' +
104+ r'instead of \(9L?, 10L?\)' )
115105
116106
117107def test_contour_shape_invalid_1 ():
@@ -123,10 +113,9 @@ def test_contour_shape_invalid_1():
123113 fig = plt .figure ()
124114 ax = fig .add_subplot (111 )
125115
126- try :
116+ with pytest . raises ( TypeError ) as excinfo :
127117 ax .contour (x , y , z )
128- except TypeError as exc :
129- assert exc .args [0 ] == 'Inputs x and y must be 1D or 2D.'
118+ excinfo .match (r'Inputs x and y must be 1D or 2D.' )
130119
131120
132121def test_contour_shape_invalid_2 ():
@@ -138,10 +127,9 @@ def test_contour_shape_invalid_2():
138127 fig = plt .figure ()
139128 ax = fig .add_subplot (111 )
140129
141- try :
130+ with pytest . raises ( TypeError ) as excinfo :
142131 ax .contour (x , y , z )
143- except TypeError as exc :
144- assert exc .args [0 ] == 'Input z must be a 2D array.'
132+ excinfo .match (r'Input z must be a 2D array.' )
145133
146134
147135@image_comparison (baseline_images = ['contour_manual_labels' ])
@@ -309,3 +297,46 @@ def test_contourf_symmetric_locator():
309297 locator = plt .MaxNLocator (nbins = 4 , symmetric = True )
310298 cs = plt .contourf (z , locator = locator )
311299 assert_array_almost_equal (cs .levels , np .linspace (- 12 , 12 , 5 ))
300+
301+
302+ def test_contour_1x1_array ():
303+ # github issue 8197
304+ with pytest .raises (TypeError ) as excinfo :
305+ plt .contour ([[0 ]])
306+ excinfo .match (r'Input z must be at least a 2x2 array.' )
307+
308+ with pytest .raises (TypeError ) as excinfo :
309+ plt .contour ([0 ], [0 ], [[0 ]])
310+ excinfo .match (r'Input z must be at least a 2x2 array.' )
311+
312+
313+ def test_internal_cpp_api ():
314+ # Following github issue 8197.
315+ import matplotlib ._contour as _contour
316+
317+ with pytest .raises (TypeError ) as excinfo :
318+ qcg = _contour .QuadContourGenerator ()
319+ excinfo .match (r'function takes exactly 6 arguments \(0 given\)' )
320+
321+ with pytest .raises (ValueError ) as excinfo :
322+ qcg = _contour .QuadContourGenerator (1 , 2 , 3 , 4 , 5 , 6 )
323+ excinfo .match (r'Expected 2-dimensional array, got 0' )
324+
325+ with pytest .raises (ValueError ) as excinfo :
326+ qcg = _contour .QuadContourGenerator ([[0 ]], [[0 ]], [[]], None , True , 0 )
327+ excinfo .match (r'x, y and z must all be 2D arrays with the same dimensions' )
328+
329+ with pytest .raises (ValueError ) as excinfo :
330+ qcg = _contour .QuadContourGenerator ([[0 ]], [[0 ]], [[0 ]], None , True , 0 )
331+ excinfo .match (r'x, y and z must all be at least 2x2 arrays' )
332+
333+ arr = [[0 , 1 ], [2 , 3 ]]
334+ with pytest .raises (ValueError ) as excinfo :
335+ qcg = _contour .QuadContourGenerator (arr , arr , arr , [[0 ]], True , 0 )
336+ excinfo .match (r'If mask is set it must be a 2D array with the same ' +
337+ r'dimensions as x.' )
338+
339+ qcg = _contour .QuadContourGenerator (arr , arr , arr , None , True , 0 )
340+ with pytest .raises (ValueError ) as excinfo :
341+ qcg .create_filled_contour (1 , 0 )
342+ excinfo .match (r'filled contour levels must be increasing' )
0 commit comments