@@ -46,10 +46,9 @@ def test_contour_shape_mismatch_1():
46
46
fig = plt .figure ()
47
47
ax = fig .add_subplot (111 )
48
48
49
- try :
49
+ with pytest . raises ( TypeError ) as excinfo :
50
50
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.' )
53
52
54
53
55
54
def test_contour_shape_mismatch_2 ():
@@ -61,10 +60,9 @@ def test_contour_shape_mismatch_2():
61
60
fig = plt .figure ()
62
61
ax = fig .add_subplot (111 )
63
62
64
- try :
63
+ with pytest . raises ( TypeError ) as excinfo :
65
64
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.' )
68
66
69
67
70
68
def test_contour_shape_mismatch_3 ():
@@ -77,15 +75,13 @@ def test_contour_shape_mismatch_3():
77
75
fig = plt .figure ()
78
76
ax = fig .add_subplot (111 )
79
77
80
- try :
78
+ with pytest . raises ( TypeError ) as excinfo :
81
79
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.' )
84
81
85
- try :
82
+ with pytest . raises ( TypeError ) as excinfo :
86
83
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.' )
89
85
90
86
91
87
def test_contour_shape_mismatch_4 ():
@@ -97,21 +93,15 @@ def test_contour_shape_mismatch_4():
97
93
fig = plt .figure ()
98
94
ax = fig .add_subplot (111 )
99
95
100
- try :
96
+ with pytest . raises ( TypeError ) as excinfo :
101
97
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?\)' )
107
100
108
- try :
101
+ with pytest . raises ( TypeError ) as excinfo :
109
102
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?\)' )
115
105
116
106
117
107
def test_contour_shape_invalid_1 ():
@@ -123,10 +113,9 @@ def test_contour_shape_invalid_1():
123
113
fig = plt .figure ()
124
114
ax = fig .add_subplot (111 )
125
115
126
- try :
116
+ with pytest . raises ( TypeError ) as excinfo :
127
117
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.' )
130
119
131
120
132
121
def test_contour_shape_invalid_2 ():
@@ -138,10 +127,9 @@ def test_contour_shape_invalid_2():
138
127
fig = plt .figure ()
139
128
ax = fig .add_subplot (111 )
140
129
141
- try :
130
+ with pytest . raises ( TypeError ) as excinfo :
142
131
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.' )
145
133
146
134
147
135
@image_comparison (baseline_images = ['contour_manual_labels' ])
@@ -309,3 +297,46 @@ def test_contourf_symmetric_locator():
309
297
locator = plt .MaxNLocator (nbins = 4 , symmetric = True )
310
298
cs = plt .contourf (z , locator = locator )
311
299
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