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

Skip to content

Commit b8ffb8c

Browse files
committed
MNT: improve the error message in Path init
1 parent 54bd6f1 commit b8ffb8c

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

lib/matplotlib/path.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,20 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1,
131131
vertices = _to_unmasked_float_array(vertices)
132132
if vertices.ndim != 2 or vertices.shape[1] != 2:
133133
raise ValueError(
134-
"'vertices' must be a 2D list or array with shape Nx2")
134+
"'vertices' must be a 2D list or array with shape (N, 2). "
135+
f"Your input has shape {vertices.shape}.")
135136

136137
if codes is not None:
137138
codes = np.asarray(codes, self.code_type)
138139
if codes.ndim != 1 or len(codes) != len(vertices):
139140
raise ValueError("'codes' must be a 1D list or array with the "
140-
"same length of 'vertices'")
141+
"same length of 'vertices'. "
142+
f"Your vertices have shape {vertices.shape} "
143+
f"but your codes have shape {codes.shape}")
141144
if len(codes) and codes[0] != self.MOVETO:
142145
raise ValueError("The first element of 'code' must be equal "
143-
"to 'MOVETO' ({})".format(self.MOVETO))
146+
f"to 'MOVETO' ({self.MOVETO}). "
147+
f"Your first code if {codes[0]}")
144148
elif closed and len(vertices):
145149
codes = np.empty(len(vertices), dtype=self.code_type)
146150
codes[0] = self.MOVETO

lib/matplotlib/tests/test_path.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import copy
2-
2+
import re
33
import numpy as np
44

55
from numpy.testing import assert_array_equal
@@ -30,6 +30,25 @@ def modify_vertices():
3030
modify_vertices()
3131

3232

33+
def test_path_exceptions():
34+
bad_verts1 = np.arange(12).reshape(4, 3)
35+
with pytest.raises(ValueError,
36+
match=re.escape(f'has shape {bad_verts1.shape}')):
37+
Path(bad_verts1)
38+
39+
bad_verts2 = np.arange(12).reshape(2, 3, 2)
40+
with pytest.raises(ValueError,
41+
match=re.escape(f'has shape {bad_verts2.shape}')):
42+
Path(bad_verts2)
43+
44+
good_verts = np.arange(12).reshape(6, 2)
45+
bad_codes = np.arange(2)
46+
msg = re.escape(f"Your vertices have shape {good_verts.shape} "
47+
f"but your codes have shape {bad_codes.shape}")
48+
with pytest.raises(ValueError, match=msg):
49+
Path(good_verts, bad_codes)
50+
51+
3352
def test_point_in_path():
3453
# Test #1787
3554
verts2 = [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]

0 commit comments

Comments
 (0)