Skip to main content

Module with functions to animate data stored in 2D+3D arrays

Project description

animate_arrays

This module wraps some setup scripts and functions to make Matplotlib’s animations a little simpler to interface with. The starting point here becomes creating data with

Install

pip install animate_arrays

How to use 2D arrays

Import libraries

The animate_arrays functions work in Jupyter notebooks. You’ll need to import some of the libraries to build some arrays, plots, and animations as such,

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML

I prefer to use the fivethirtyeight style sheet, but you can use any style.

plt.style.use('fivethirtyeight')

Then, import the animate_lines function from the animate_arrays.animate module.

from animate_arrays.animate import animate_lines

Build arrays and watch them move

With the libraries imported, I build 2 arrays, X and Y where each column is $\times 3~(x,~y)$ coordinates that define two rotating arms.

  • Arm 1 rotates once around the origin
  • Arm 2 is connected to arm 1 and rotates twice
a = np.linspace(0, 2*np.pi, 100)

x1 = np.cos(a)
y1 = np.sin(a)

x2 = np.cos(2*a)
y2 = np.sin(2*a)
X = np.array([np.zeros(len(a)), x1, x1+x2])
Y = np.array([np.zeros(len(a)), y1, y1+y2])
a = animate_lines(X, Y)
HTML(a.to_html5_video())
Your browser does not support the video tag.

The animation does not have equal axes, so the arms appear to be changing lengths as the rotations occur.

The setup_fig_function option allows you to define a custom plot setup. Here, I name it fig_setup

def fig_setup():
    fig, ax = plt.subplots()
    
    ax.plot(X[2, :], Y[2, :], '--')
    ax.axis('equal')
    return fig, ax

The setup_fig_function should not take any arguments. I used it to set up 2 new display additions, 1. plot the path of arm 2’s end so we can see where its been and where its going on the dashed line 2. equal axes with ax.axis('equal')

Below, I try running the function and looking at the resulting static figure for the animation

fig, ax = fig_setup()

a = animate_lines(X, Y, setup_fig_function= fig_setup)
HTML(a.to_html5_video())
Your browser does not support the video tag.

How to use 3D arrays

In the first example, we used a 2D array where each column defined the line for a point in time. The animate_lines function can also plot multiple lines in each frame. Here, we’ll add another set of rotating arms.

a = np.linspace(0, 2*np.pi, 100)

x1 = np.cos(a)
y1 = np.sin(a)

x2 = np.cos(2*a)
y2 = np.sin(2*a)

x3 = np.cos(2*a)
y3 = np.sin(2*a)

x4 = np.cos(3*a)
y4 = np.sin(3*a)

X = np.zeros((3, 2, len(a)))
Y = np.zeros((3, 2, len(a)))

X[:, 0, :] = np.array([np.zeros(len(a)), x1, x1+x2])
X[:, 1, :] = np.array([np.zeros(len(a)), x3, x3+x4])

Y[:, 0, :] = np.array([np.zeros(len(a)), y1, y1+y2])
Y[:, 1, :] = np.array([np.zeros(len(a)), y3, y3+y4])
def fig_setup():
    fig, ax = plt.subplots()
    
    ax.plot(X[2, 0, :], Y[2, 0, :], '--')
    ax.plot(X[2, 1, :], Y[2, 1, :], '--')
    ax.axis('equal')
    return fig, ax
fig_setup()
(<Figure size 640x480 with 1 Axes>, <AxesSubplot: >)

a2 = animate_lines(X, Y, setup_fig_function=fig_setup)
HTML(a2.to_html5_video())
Your browser does not support the video tag.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

animate_arrays-0.0.1.tar.gz (998.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

animate_arrays-0.0.1-py3-none-any.whl (339.4 kB view details)

Uploaded Python 3

File details

Details for the file animate_arrays-0.0.1.tar.gz.

File metadata

  • Download URL: animate_arrays-0.0.1.tar.gz
  • Upload date:
  • Size: 998.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.3 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.8.15

File hashes

Hashes for animate_arrays-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1a248cdbf88b1627bd2b6a0e222ab30e83c879ee60e25495b77128d0efca76ed
MD5 01f3bd7a5b6facb3fc9a8fdacb4c8e39
BLAKE2b-256 89d35576c44d583d6140e2d2f2eadbbbb26c3c9d471c72c2a12e4a8991abc1ec

See more details on using hashes here.

File details

Details for the file animate_arrays-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: animate_arrays-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 339.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.3 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.8.15

File hashes

Hashes for animate_arrays-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7e70165b704a6b10978280155feaab87f6326fa433e454b3652d23dbadda459
MD5 48142fec191ea03f55f6316001a9424f
BLAKE2b-256 16fe5be7fd7beb4a4103ddfdb116ea4ce45180556a8f94a669f367031543fe85

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page