Skip to content

Commit b7e4b8c

Browse files
committed
polish quiver, pcolormesh and release
1 parent bcba1fa commit b7e4b8c

12 files changed

+973
-43
lines changed

animatplot/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version = (0, 2, 0)
2-
info = '.dev2'
2+
info = ''
33
__version__ = '.'.join(map(str, version))+info

animatplot/blocks/image_like.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ class Pcolormesh(Block):
88
99
Parameters
1010
----------
11-
X, Y : 2D np.ndarray, optional
12-
C : 3D np.ndarray
13-
axis : matplotlib axis
11+
X : 2D np.ndarray, optional
12+
Y : 2D np.ndarray, optional
13+
C : list of 2D np.ndarray or a 3D np.ndarray
14+
axis : matplotlib axis, optional
15+
an axis to attach the block to.
16+
t_axis : int, optional
17+
The axis of the array that represents time. Defaults to 0.
18+
No effect if C is a list.
1419
1520
Notes
1621
-----
@@ -29,24 +34,34 @@ def __init__(self, *args, axis=None, t_axis=0, **kwargs):
2934
else:
3035
raise TypeError(
3136
'Illegal arguments to pcolormesh; see help(pcolormesh)')
32-
if len(self.C.shape) != 3:
33-
raise TypeError('C must be a 3D array')
3437

35-
super().__init__(axis)
38+
super().__init__(axis, t_axis)
39+
40+
self._is_list = isinstance(self.C, list)
41+
self.C = np.asanyarray(self.C)
3642

43+
Slice = self._make_slice(0, 3)
3744
if self._arg_len == 1:
38-
self.quad = self.ax.pcolormesh(self.C[:, :, 0], **kwargs)
45+
self.quad = self.ax.pcolormesh(self.C[Slice], **kwargs)
3946
elif self._arg_len == 3:
40-
self.quad = self.ax.pcolormesh(self.X, self.Y, self.C[:, :, 0],
47+
self.quad = self.ax.pcolormesh(self.X, self.Y, self.C[Slice],
4148
**kwargs)
4249

4350
def _update(self, i):
44-
self.quad.set_array(self.C[:-1, :-1, i].ravel())
51+
Slice = self._make_pcolormesh_slice(i, 3)
52+
self.quad.set_array(self.C[Slice].ravel())
4553
return self.quad
4654

4755
def __len__(self):
4856
return self.C.shape[2]
4957

58+
def _make_pcolormesh_slice(self, i, dim):
59+
if self._is_list:
60+
return i
61+
Slice = [slice(-1)]*3 # weird thing to make animation work
62+
Slice[self.t_axis] = i
63+
return tuple(Slice)
64+
5065

5166
class Imshow(Block):
5267
"""Animates a series of images
@@ -64,6 +79,7 @@ class Imshow(Block):
6479
t_axis : int, optional
6580
The axis of the array that represents time. Defaults to 0.
6681
No effect if images is a list.
82+
6783
Notes
6884
-----
6985
This block accepts additional keyword arguments to be passed to

animatplot/blocks/vectors.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .base import Block
2+
import numpy as np
23

34

45
class Quiver(Block):
@@ -7,48 +8,53 @@ class Quiver(Block):
78
89
Parameters
910
----------
10-
X, Y : 2D or 3D numpy array
11-
U, V : 3D numpy array
11+
X : 1D or 2D numpy array
12+
The x positions of the arrows. Cannot be animated.
13+
Y : 1D or 2D numpy array
14+
The y positions of the arrows. Cannot be animated.
15+
U : 2D or 3D numpy array
16+
The U displacement of the arrows. 1 dimension
17+
higher than the X, Y arrays.
18+
V : 2D or 3D numpy array
19+
The V displcement of the arrows. 1 dimension
20+
higher than the X, Y arrays.
1221
axis : matplotlib axis, optional
1322
The axis to the block to
23+
t_axis : int, optional
24+
The axis of the array that represents time. Defaults to 0.
25+
No effect if U, V are lists.
26+
1427
Notes
1528
-----
1629
This block accepts additional keyword arguments to be passed to
1730
:meth:`matplotlib.axes.Axes.quiver`
1831
"""
19-
def __init__(self, X, Y, U, V, axis=None, **kwargs):
32+
def __init__(self, X, Y, U, V, axis=None, t_axis=0, **kwargs):
33+
self.X = X
34+
self.Y = Y
35+
self.U = np.asanyarray(U)
36+
self.V = np.asanyarray(V)
2037
if X.shape != Y.shape:
2138
raise ValueError("X, Y must have the same shape")
22-
if U.shape != V.shape:
39+
if self.U.shape != self.V.shape:
2340
raise ValueError("U, V must have the same shape")
2441

25-
# lots of features removed b/c quiver plots can't set_XY
26-
# self.animate_XY = len(X.shape) == 3
27-
# self.animate_UV = len(U.shape) == 3
28-
29-
self.x = X
30-
self.y = Y
31-
self.u = U
32-
self.v = V
33-
self.ax = axis
42+
super().__init__(axis, t_axis)
3443

35-
xy_slice = [slice(None)]*2 + ([slice(1)] if len(X.shape) == 3 else [])
36-
# uv_slice = [slice(None)]*2 +([slice(1)] if len(U.shape) == 3 else [])
44+
self._dim = len(self.U.shape)
45+
self._is_list = isinstance(U, list)
3746

38-
self.Q = self.ax.quiver(X[tuple(xy_slice)].squeeze(),
39-
Y[tuple(xy_slice)].squeeze(),
40-
U[:, :, 0], V[:, :, 0],
47+
Slice = self._make_slice(0, self._dim)
48+
self.Q = self.ax.quiver(self.X, self.Y,
49+
self.U[Slice], self.V[Slice],
4150
**kwargs)
4251

4352
def _update(self, i):
44-
# if self.animate_UV:
45-
self.Q.set_UVC(self.u[:, :, i], self.v[:, :, i])
46-
# if self.animate_XY:
47-
# pass # self.Q.set_XYC(self.x[:, :, i], self.y[:, :, i])
48-
return self.Q,
53+
Slice = self._make_slice(i, self._dim)
54+
self.Q.set_UVC(self.U[Slice], self.V[Slice])
55+
return self.Q
4956

5057
def __len__(self):
51-
# if len(self.x.shape) == 3:
52-
# return self.x.shape[2]
53-
# else:
54-
return self.u.shape[2]
58+
if self._is_list:
59+
return self.U.shape[0]
60+
return self.U.shape[self.t_axis]

docs/source/gallery/Nuke.ipynb

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

docs/source/gallery/nuke.gif

3.12 MB
Loading

docs/source/gallery/pcolormesh.gif

760 Bytes
Loading

docs/source/gallery/pcolormesh.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"\n",
3939
"Z = np.sin(X*X+Y*Y-T)\n",
4040
"\n",
41-
"block = amp.blocks.Pcolormesh(X[:,:,0], Y[:,:,0], Z, cmap='RdBu')\n",
41+
"block = amp.blocks.Pcolormesh(X[:,:,0], Y[:,:,0], Z, t_axis=2, cmap='RdBu')\n",
4242
"plt.colorbar(block.quad)\n",
4343
"plt.gca().set_aspect('equal')\n",
4444
"\n",

docs/source/gallery/polarization.gif

-1.44 MB
Loading

0 commit comments

Comments
 (0)