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 4910610

Browse filesBrowse files
committed
Improved checking logic of _check_xyz in contour.py, added tests for exceptions
1 parent 17e1cf8 commit 4910610
Copy full SHA for 4910610

File tree

Expand file treeCollapse file tree

3 files changed

+157
-9
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+157
-9
lines changed

‎lib/matplotlib/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/__init__.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ def tk_window_focus():
10771077
'matplotlib.tests.test_cbook',
10781078
'matplotlib.tests.test_colorbar',
10791079
'matplotlib.tests.test_colors',
1080+
'matplotlib.tests.test_contour',
10801081
'matplotlib.tests.test_dates',
10811082
'matplotlib.tests.test_delaunay',
10821083
'matplotlib.tests.test_figure',

‎lib/matplotlib/contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/contour.py
+29-9Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,20 +1424,40 @@ def _check_xyz(self, args, kwargs):
14241424
x = np.asarray(x, dtype=np.float64)
14251425
y = np.asarray(y, dtype=np.float64)
14261426
z = ma.asarray(args[2], dtype=np.float64)
1427+
14271428
if z.ndim != 2:
14281429
raise TypeError("Input z must be a 2D array.")
14291430
else:
14301431
Ny, Nx = z.shape
1431-
if x.shape == z.shape and y.shape == z.shape:
1432-
return x, y, z
1433-
if x.ndim != 1 or y.ndim != 1:
1432+
1433+
if x.ndim != y.ndim:
1434+
raise TypeError("Number of dimensions of x and y should match.")
1435+
1436+
if x.ndim == 1:
1437+
1438+
nx, = x.shape
1439+
ny, = y.shape
1440+
1441+
if nx != Nx:
1442+
raise TypeError("Length of x must be number of columns in z.")
1443+
1444+
if ny != Ny:
1445+
raise TypeError("Length of y must be number of rows in z.")
1446+
1447+
x, y = np.meshgrid(x, y)
1448+
1449+
elif x.ndim == 2:
1450+
1451+
if x.shape != z.shape:
1452+
raise TypeError("Shape of x does not match that of z: found {0} instead of {1}.".format(x.shape, z.shape))
1453+
1454+
if y.shape != z.shape:
1455+
raise TypeError("Shape of y does not match that of z: found {0} instead of {1}.".format(y.shape, z.shape))
1456+
1457+
else:
1458+
14341459
raise TypeError("Inputs x and y must be 1D or 2D.")
1435-
nx, = x.shape
1436-
ny, = y.shape
1437-
if nx != Nx or ny != Ny:
1438-
raise TypeError("Length of x must be number of columns in z,\n" +
1439-
"and length of y must be number of rows.")
1440-
x, y = np.meshgrid(x, y)
1460+
14411461
return x, y, z
14421462

14431463
def _initialize_x_y(self, z):

‎lib/matplotlib/tests/test_contour.py

Copy file name to clipboard
+127Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import numpy as np
2+
from matplotlib import pyplot as plt
3+
4+
5+
def test_contour_shape_1d_valid():
6+
7+
x = np.arange(10)
8+
y = np.arange(9)
9+
z = np.random.random((9, 10))
10+
11+
fig = plt.figure()
12+
ax = fig.add_subplot(111)
13+
ax.contour(x, y, z)
14+
15+
16+
def test_contour_shape_2d_valid():
17+
18+
x = np.arange(10)
19+
y = np.arange(9)
20+
xg, yg = np.meshgrid(x, y)
21+
z = np.random.random((9, 10))
22+
23+
fig = plt.figure()
24+
ax = fig.add_subplot(111)
25+
ax.contour(xg, yg, z)
26+
27+
28+
def test_contour_shape_mismatch_1():
29+
30+
x = np.arange(9)
31+
y = np.arange(9)
32+
z = np.random.random((9, 10))
33+
34+
fig = plt.figure()
35+
ax = fig.add_subplot(111)
36+
37+
try:
38+
ax.contour(x, y, z)
39+
except TypeError as exc:
40+
assert exc.args[0] == 'Length of x must be number of columns in z.'
41+
42+
43+
def test_contour_shape_mismatch_2():
44+
45+
x = np.arange(10)
46+
y = np.arange(10)
47+
z = np.random.random((9, 10))
48+
49+
fig = plt.figure()
50+
ax = fig.add_subplot(111)
51+
52+
try:
53+
ax.contour(x, y, z)
54+
except TypeError as exc:
55+
assert exc.args[0] == 'Length of y must be number of rows in z.'
56+
57+
58+
def test_contour_shape_mismatch_3():
59+
60+
x = np.arange(10)
61+
y = np.arange(10)
62+
xg, yg = np.meshgrid(x, y)
63+
z = np.random.random((9, 10))
64+
65+
fig = plt.figure()
66+
ax = fig.add_subplot(111)
67+
68+
try:
69+
ax.contour(xg, y, z)
70+
except TypeError as exc:
71+
assert exc.args[0] == 'Number of dimensions of x and y should match.'
72+
73+
try:
74+
ax.contour(x, yg, z)
75+
except TypeError as exc:
76+
assert exc.args[0] == 'Number of dimensions of x and y should match.'
77+
78+
79+
def test_contour_shape_mismatch_4():
80+
81+
g = np.random.random((9, 10))
82+
b = np.random.random((9, 9))
83+
z = np.random.random((9, 10))
84+
85+
fig = plt.figure()
86+
ax = fig.add_subplot(111)
87+
88+
try:
89+
ax.contour(b, g, z)
90+
except TypeError as exc:
91+
print exc.args[0]
92+
assert exc.args[0] == 'Shape of x does not match that of z: found (9, 9) instead of (9, 10).'
93+
94+
try:
95+
ax.contour(g, b, z)
96+
except TypeError as exc:
97+
assert exc.args[0] == 'Shape of y does not match that of z: found (9, 9) instead of (9, 10).'
98+
99+
100+
def test_contour_shape_invalid_1():
101+
102+
x = np.random.random((3, 3, 3))
103+
y = np.random.random((3, 3, 3))
104+
z = np.random.random((9, 10))
105+
106+
fig = plt.figure()
107+
ax = fig.add_subplot(111)
108+
109+
try:
110+
ax.contour(x, y, z)
111+
except TypeError as exc:
112+
assert exc.args[0] == 'Inputs x and y must be 1D or 2D.'
113+
114+
115+
def test_contour_shape_invalid_2():
116+
117+
x = np.random.random((3, 3, 3))
118+
y = np.random.random((3, 3, 3))
119+
z = np.random.random((3, 3, 3))
120+
121+
fig = plt.figure()
122+
ax = fig.add_subplot(111)
123+
124+
try:
125+
ax.contour(x, y, z)
126+
except TypeError as exc:
127+
assert exc.args[0] == 'Input z must be a 2D array.'

0 commit comments

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