From 38e3afec8717fa50816a9d21415e1461cef7d3db Mon Sep 17 00:00:00 2001 From: jaideep Date: Mon, 27 Aug 2018 13:24:02 +0530 Subject: [PATCH 1/4] try 2 --- lib/matplotlib/quiver.py | 76 +++++++++++++++++++++-------- lib/matplotlib/tests/test_quiver.py | 19 ++++++++ 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 9b63e7734198..8718f03754a6 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -381,30 +381,54 @@ def contains(self, mouseevent): # This is a helper function that parses out the various combination of # arguments for doing colored vector plots. Pulling it out here # allows both Quiver and Barbs to use it -def _parse_args(*args): +def _parse_args(*args, **kw): X, Y, U, V, C = [None] * 5 - args = list(args) - - # The use of atleast_1d allows for handling scalar arguments while also - # keeping masked arrays - if len(args) == 3 or len(args) == 5: - C = np.atleast_1d(args.pop(-1)) - V = np.atleast_1d(args.pop(-1)) - U = np.atleast_1d(args.pop(-1)) - if U.ndim == 1: - nr, nc = 1, U.shape[0] - else: - nr, nc = U.shape - if len(args) == 2: # remaining after removing U,V,C - X, Y = [np.array(a).ravel() for a in args] - if len(X) == nc and len(Y) == nr: - X, Y = [a.ravel() for a in np.meshgrid(X, Y)] - else: - indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) - X, Y = [np.ravel(a) for a in indexgrid] + args = list(args) # The use of atleast_1d allows for handling + if len(args) != 0: # scalar arguments while also + if len(args) == 3 or len(args) == 5: # keeping masked arrays + C = np.atleast_1d(args.pop(-1)) + V = np.atleast_1d(args.pop(-1)) + U = np.atleast_1d(args.pop(-1)) + if U.ndim == 1: + nr, nc = 1, U.shape[0] + else: + nr, nc = U.shape + if len(args) == 2: # remaining after removing U,V,C + X, Y = [np.array(a).ravel() for a in args] + if len(X) == nc and len(Y) == nr: + X, Y = [a.ravel() for a in np.meshgrid(X, Y)] + else: + indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) + X, Y = [np.ravel(a) for a in indexgrid] + if len(kw) != 0: # some keyword arguments + # The use of atleast_1d allows for handling scalar arguments while also + # keeping masked arrays + if len(kw) == 3 or len(kw) == 5: + if (kw.get('C') is not None): + C = np.atleast_1d(kw.pop('C')) + if (kw.get('V') is not None): + V = np.atleast_1d(kw.pop('V')) + if (kw.get('U') is not None): + U = np.atleast_1d(kw.pop('U')) + if U.ndim == 1: + nr, nc = 1, U.shape[0] + else: + nr, nc = U.shape + if len(kw) == 2: # remaining after removing U,V,C. CASE 1 + if kw.get('X') is not None: + X = np.array(kw.get('X')).ravel() + if kw.get('Y') is not None: + Y = np.array(kw.get('Y')).ravel() + if len(X) == nc and len(Y) == nr: + X, Y = [a.ravel() for a in np.meshgrid(X, Y)] + elif len(kw) == 0: # CASE 2 + indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) + X, Y = [np.ravel(a) for a in indexgrid] return X, Y, U, V, C + + def _check_consistent_shapes(*arrays): all_shapes = {a.shape for a in arrays} if len(all_shapes) != 1: @@ -443,7 +467,17 @@ def __init__(self, ax, *args, %s """ self.ax = ax - X, Y, U, V, C = _parse_args(*args) + X, Y, U, V, C = _parse_args(*args, **kw) + if kw.get('U') is not None: # Resetting **kw to the way it was without these + kw.pop('U') + if kw.get('V') is not None: + kw.pop('V') + if kw.get('X') is not None: + kw.pop('X') + if kw.get('Y') is not None: + kw.pop('Y') + if kw.get('C') is not None: + kw.pop('C') self.X = X self.Y = Y self.XY = np.column_stack((X, Y)) diff --git a/lib/matplotlib/tests/test_quiver.py b/lib/matplotlib/tests/test_quiver.py index 4470e02fac8c..79fbedc48c6d 100644 --- a/lib/matplotlib/tests/test_quiver.py +++ b/lib/matplotlib/tests/test_quiver.py @@ -199,3 +199,22 @@ def test_quiverkey_angles(): # The arrows are only created when the key is drawn fig.canvas.draw() assert len(qk.verts) == 1 + +def test_quiver_keyword_arguments(): + ax = plt.axes() + x, y = np.arange(8), np.arange(10) + u = v = np.linspace(0, 10, 80).reshape(10, 8) + q = plt.quiver(X = x, Y = y, U = u, V = v) + assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) + + ax = plt.axes() + x, y = np.arange(8), np.arange(10) + u = v = np.linspace(0, 10, 80).reshape(10, 8) + q = plt.quiver(x, y, U = u, V = v) + assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) + + ax = plt.axes() + x, y = np.arange(8), np.arange(10) + u = v = np.linspace(0, 10, 80).reshape(10, 8) + q = plt.quiver(X = x, Y = y, U = u, V = v, C = (1, 1, 1)) + assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) \ No newline at end of file From 97b26424daab340fb4f12f554733799454148224 Mon Sep 17 00:00:00 2001 From: jaideep Date: Mon, 27 Aug 2018 13:46:13 +0530 Subject: [PATCH 2/4] try 2 --- lib/matplotlib/quiver.py | 12 +++++++++++- lib/matplotlib/tests/test_quiver.py | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 8718f03754a6..947f1ff60d07 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -984,7 +984,17 @@ def __init__(self, ax, *args, kw['linewidth'] = 1 # Parse out the data arrays from the various configurations supported - x, y, u, v, c = _parse_args(*args) + x, y, u, v, c = _parse_args(*args, **kw) + if kw.get('U') is not None: # Resetting **kw to the way it was without these + kw.pop('U') + if kw.get('V') is not None: + kw.pop('V') + if kw.get('X') is not None: + kw.pop('X') + if kw.get('Y') is not None: + kw.pop('Y') + if kw.get('C') is not None: + kw.pop('C') self.x = x self.y = y xy = np.column_stack((x, y)) diff --git a/lib/matplotlib/tests/test_quiver.py b/lib/matplotlib/tests/test_quiver.py index 79fbedc48c6d..534efb771f9d 100644 --- a/lib/matplotlib/tests/test_quiver.py +++ b/lib/matplotlib/tests/test_quiver.py @@ -217,4 +217,5 @@ def test_quiver_keyword_arguments(): x, y = np.arange(8), np.arange(10) u = v = np.linspace(0, 10, 80).reshape(10, 8) q = plt.quiver(X = x, Y = y, U = u, V = v, C = (1, 1, 1)) - assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) \ No newline at end of file + assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) + From f8006b606e2aad350f47b98559beb5017ac93c33 Mon Sep 17 00:00:00 2001 From: jaideep Date: Thu, 30 Aug 2018 13:11:04 +0530 Subject: [PATCH 3/4] flake8 errors --- lib/matplotlib/quiver.py | 14 ++++++-------- lib/matplotlib/tests/test_quiver.py | 10 +++++----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 947f1ff60d07..e062ce1c817f 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -427,8 +427,6 @@ def _parse_args(*args, **kw): return X, Y, U, V, C - - def _check_consistent_shapes(*arrays): all_shapes = {a.shape for a in arrays} if len(all_shapes) != 1: @@ -468,9 +466,9 @@ def __init__(self, ax, *args, """ self.ax = ax X, Y, U, V, C = _parse_args(*args, **kw) - if kw.get('U') is not None: # Resetting **kw to the way it was without these - kw.pop('U') - if kw.get('V') is not None: + if kw.get('U') is not None: # Resetting **kw to the + kw.pop('U') # way it was without these + if kw.get('V') is not None: kw.pop('V') if kw.get('X') is not None: kw.pop('X') @@ -985,9 +983,9 @@ def __init__(self, ax, *args, # Parse out the data arrays from the various configurations supported x, y, u, v, c = _parse_args(*args, **kw) - if kw.get('U') is not None: # Resetting **kw to the way it was without these - kw.pop('U') - if kw.get('V') is not None: + if kw.get('U') is not None: # Resetting **kw to the way + kw.pop('U') # it was without these + if kw.get('V') is not None: kw.pop('V') if kw.get('X') is not None: kw.pop('X') diff --git a/lib/matplotlib/tests/test_quiver.py b/lib/matplotlib/tests/test_quiver.py index 534efb771f9d..5036744ecf07 100644 --- a/lib/matplotlib/tests/test_quiver.py +++ b/lib/matplotlib/tests/test_quiver.py @@ -200,22 +200,22 @@ def test_quiverkey_angles(): fig.canvas.draw() assert len(qk.verts) == 1 + def test_quiver_keyword_arguments(): ax = plt.axes() x, y = np.arange(8), np.arange(10) u = v = np.linspace(0, 10, 80).reshape(10, 8) - q = plt.quiver(X = x, Y = y, U = u, V = v) + q = plt.quiver(X=x, Y=y, U=u, V=v) assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) ax = plt.axes() x, y = np.arange(8), np.arange(10) u = v = np.linspace(0, 10, 80).reshape(10, 8) - q = plt.quiver(x, y, U = u, V = v) + q = plt.quiver(x, y, U=u, V=v) assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) ax = plt.axes() x, y = np.arange(8), np.arange(10) u = v = np.linspace(0, 10, 80).reshape(10, 8) - q = plt.quiver(X = x, Y = y, U = u, V = v, C = (1, 1, 1)) - assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) - + q = plt.quiver(X=x, Y=y, U=u, V=v, C=(1, 1, 1)) + assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) \ No newline at end of file From 3e580e68e619ad27060c0192c3ef41e0bc3f4505 Mon Sep 17 00:00:00 2001 From: jaideep Date: Thu, 30 Aug 2018 14:14:25 +0530 Subject: [PATCH 4/4] flak8 errors(2) --- lib/matplotlib/tests/test_quiver.py | 32 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/tests/test_quiver.py b/lib/matplotlib/tests/test_quiver.py index 5036744ecf07..eaaa7857df47 100644 --- a/lib/matplotlib/tests/test_quiver.py +++ b/lib/matplotlib/tests/test_quiver.py @@ -186,21 +186,6 @@ def test_quiver_xy(): ax.grid() -def test_quiverkey_angles(): - # Check that only a single arrow is plotted for a quiverkey when an array - # of angles is given to the original quiver plot - fig, ax = plt.subplots() - - X, Y = np.meshgrid(np.arange(2), np.arange(2)) - U = V = angles = np.ones_like(X) - - q = ax.quiver(X, Y, U, V, angles=angles) - qk = ax.quiverkey(q, 1, 1, 2, 'Label') - # The arrows are only created when the key is drawn - fig.canvas.draw() - assert len(qk.verts) == 1 - - def test_quiver_keyword_arguments(): ax = plt.axes() x, y = np.arange(8), np.arange(10) @@ -218,4 +203,19 @@ def test_quiver_keyword_arguments(): x, y = np.arange(8), np.arange(10) u = v = np.linspace(0, 10, 80).reshape(10, 8) q = plt.quiver(X=x, Y=y, U=u, V=v, C=(1, 1, 1)) - assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) \ No newline at end of file + assert q.get_datalim(ax.transData).bounds == (0., 0., 7., 9.) + + +def test_quiverkey_angles(): + # Check that only a single arrow is plotted for a quiverkey when an array + # of angles is given to the original quiver plot + fig, ax = plt.subplots() + + X, Y = np.meshgrid(np.arange(2), np.arange(2)) + U = V = angles = np.ones_like(X) + + q = ax.quiver(X, Y, U, V, angles=angles) + qk = ax.quiverkey(q, 1, 1, 2, 'Label') + # The arrows are only created when the key is drawn + fig.canvas.draw() + assert len(qk.verts) == 1