Added covariance ellipse animatoin.
This commit is contained in:
parent
ee15475c3d
commit
d8336ceae7
@ -1,7 +1,7 @@
|
||||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:1ca55361c7aabbb0d220744306919e1ec1a557e86f8de4612e37ee74aa2390ae"
|
||||
"signature": "sha256:5d9e7c75a6f88ccdf422d155a9c5fb789b7b07e02f1a84790c3c8191465f131b"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
@ -252,6 +252,66 @@
|
||||
],
|
||||
"prompt_number": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"def plot_3d_covariance(ax, mean, cov):\n",
|
||||
" \"\"\" plots a 2x2 covariance matrix positioned at mean. mean will be plotted\n",
|
||||
" in x and y, and the probability in the z axis.\n",
|
||||
"\n",
|
||||
" Parameters\n",
|
||||
" ----------\n",
|
||||
" mean : 2x1 tuple-like object\n",
|
||||
" mean for x and y coordinates. For example (2.3, 7.5)\n",
|
||||
"\n",
|
||||
" cov : 2x2 nd.array\n",
|
||||
" the covariance matrix\n",
|
||||
"\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" # compute width and height of covariance ellipse so we can choose\n",
|
||||
" # appropriate ranges for x and y\n",
|
||||
" o,w,h = stats.covariance_ellipse(cov,3)\n",
|
||||
" # rotate width and height to x,y axis\n",
|
||||
" wx = abs(w*np.cos(o) + h*np.sin(o))*1.2\n",
|
||||
" wy = abs(h*np.cos(o) - w*np.sin(o))*1.2\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" # ensure axis are of the same size so everything is plotted with the same\n",
|
||||
" # scale\n",
|
||||
" if wx > wy:\n",
|
||||
" w = wx\n",
|
||||
" else:\n",
|
||||
" w = wy\n",
|
||||
"\n",
|
||||
" minx = mean[0] - w\n",
|
||||
" maxx = mean[0] + w\n",
|
||||
" miny = mean[1] - w\n",
|
||||
" maxy = mean[1] + w\n",
|
||||
"\n",
|
||||
" xs = np.arange(minx, maxx, (maxx-minx)/40.)\n",
|
||||
" ys = np.arange(miny, maxy, (maxy-miny)/40.)\n",
|
||||
" xv, yv = np.meshgrid (xs, ys)\n",
|
||||
"\n",
|
||||
" zs = np.array([100.* stats.multivariate_gaussian(np.array([x,y]),mean,cov) \\\n",
|
||||
" for x,y in zip(np.ravel(xv), np.ravel(yv))])\n",
|
||||
" zv = zs.reshape(xv.shape)\n",
|
||||
"\n",
|
||||
" ax = plt.figure().add_subplot(111, projection='3d')\n",
|
||||
" ax.plot_surface(xv, yv, zv, rstride=1, cstride=1, cmap=cm.autumn)\n",
|
||||
"\n",
|
||||
" ax.set_xlabel('X')\n",
|
||||
" ax.set_ylabel('Y')\n",
|
||||
"\n",
|
||||
" ax.contour(xv, yv, zv, zdir='x', offset=minx-1, cmap=cm.autumn)\n",
|
||||
" ax.contour(xv, yv, zv, zdir='y', offset=maxy, cmap=cm.BuGn)\n"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"prompt_number": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
|
File diff suppressed because one or more lines are too long
398
Chapter06_Multivariate_Kalman_Filter/animations.ipynb
Normal file
398
Chapter06_Multivariate_Kalman_Filter/animations.ipynb
Normal file
File diff suppressed because one or more lines are too long
BIN
Chapter06_Multivariate_Kalman_Filter/ellipse.gif
Normal file
BIN
Chapter06_Multivariate_Kalman_Filter/ellipse.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 403 KiB |
@ -8,7 +8,7 @@ Created on Sun Sep 14 12:41:24 2014
|
||||
from matplotlib import animation
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def animate(filename, func, frames, interval, figsize=(6.5, 6.5)):
|
||||
def animate(filename, func, frames, interval, fig=None, figsize=(6.5, 6.5)):
|
||||
""" Creates an animated GIF of a matplotlib.
|
||||
|
||||
Parameters
|
||||
@ -43,7 +43,8 @@ def animate(filename, func, frames, interval, figsize=(6.5, 6.5)):
|
||||
return
|
||||
func(frame)
|
||||
|
||||
fig = plt.figure(figsize=figsize)
|
||||
if fig is None:
|
||||
fig = plt.figure(figsize=figsize)
|
||||
forward.first = True
|
||||
anim = animation.FuncAnimation(fig, forward, frames=frames, interval=interval)
|
||||
anim.save(filename, writer='imagemagick')
|
Loading…
Reference in New Issue
Block a user