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 c0217db

Browse filesBrowse files
committed
Support kwargs in contour
1 parent cc85fb6 commit c0217db
Copy full SHA for c0217db

File tree

Expand file treeCollapse file tree

2 files changed

+40
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+40
-3
lines changed

‎lib/matplotlib/contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/contour.py
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ def _process_args(self, *args, corner_mask=None, algorithm=None, **kwargs):
13861386
"""
13871387
Process args and kwargs.
13881388
"""
1389-
if isinstance(args[0], QuadContourSet):
1389+
if args and isinstance(args[0], QuadContourSet):
13901390
if self.levels is None:
13911391
self.levels = args[0].levels
13921392
self.zmin = args[0].zmin
@@ -1446,11 +1446,26 @@ def _contour_args(self, args, kwargs):
14461446
else:
14471447
fn = 'contour'
14481448
nargs = len(args)
1449-
if nargs <= 2:
1449+
if "X" in kwargs and "Y" in kwargs and "Z" in kwargs:
1450+
x, y, z = self._check_xyz(
1451+
[
1452+
kwargs.pop("X"),
1453+
kwargs.pop("Y"),
1454+
kwargs.pop("Z")
1455+
],
1456+
kwargs
1457+
)
1458+
elif (("X" in kwargs) ^ ("Y" in kwargs)) and "Z" in kwargs:
1459+
raise KeyError("Both 'X' and 'Y' must be passed as kwargs. "
1460+
"Passing only one of the two is not allowed.")
1461+
elif "Z" in kwargs:
1462+
z = ma.asarray(kwargs.pop("Z"), dtype=np.float64)
1463+
x, y = self._initialize_x_y(z)
1464+
elif 0 < nargs <= 2:
14501465
z = ma.asarray(args[0], dtype=np.float64)
14511466
x, y = self._initialize_x_y(z)
14521467
args = args[1:]
1453-
elif nargs <= 4:
1468+
elif 2 < nargs <= 4:
14541469
x, y, z = self._check_xyz(args[:3], kwargs)
14551470
args = args[3:]
14561471
else:

‎lib/matplotlib/tests/test_contour.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_contour.py
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,25 @@ def test_contour_remove():
693693
assert ax.get_children() != orig_children
694694
cs.remove()
695695
assert ax.get_children() == orig_children
696+
697+
698+
def test_contour_z_kwargonly():
699+
# Smoke test for GH#24743
700+
# Passing only kwargs
701+
plt.contour(Z=np.random.rand(30, 30))
702+
703+
704+
def test_contour_xyz_kwargsonly():
705+
plt.contour(
706+
X=np.random.rand(30, 30),
707+
Y=np.random.rand(30, 30),
708+
Z=np.random.rand(30, 30)
709+
)
710+
711+
712+
def test_contour_either_xy():
713+
with pytest.raises(KeyError):
714+
plt.contour(
715+
X=np.random.rand(30, 30),
716+
Z=np.random.rand(30, 30)
717+
)

0 commit comments

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