Skip to main content

Simple and effective matplotlib animation wrapper

Project description

Synimatic

PyPI version Build Status License: MIT Python Versions

Synimatic simplifies animation creation by wrapping matplotlib's FuncAnimation. It removes repetitive setup, detects the execution environment (Jupyter or terminal), features progress indicators, and offers straightforward methods to display or save animations.


Features

  • Easy Setup: Define animation logic clearly through setup and update functions.
  • Environment Detection: Automatically renders inline in notebooks or in separate windows in terminals.
  • Progress Display: Shows progress bars during rendering and saving processes.
  • Simple API: .show() and .save() methods for effortless display or export.

Installation

pip3 install synimatic

Core Concepts

Animations require two key functions:

  1. setup(): Executes once to establish the look of a static plot elements like figure, axes, and artists to animate. Returns (fig, ax, *artists).

  2. update(frame, context): Called each frame to update plot elements.

    • frame: Current frame index or value.
    • context: Tuple of objects returned by setup.

Usage Example

Graph Update :

import numpy as np
import matplotlib.pyplot as plt
from synimatic import Synimator

def setup():
    fig, ax = plt.subplots()
    ax.set_xlim(0, 4 * np.pi)
    ax.set_ylim(-2.2, 2.2)
    line, = ax.plot([], [], lw=2)
    return fig, ax, line

def update(frame, context):
    fig, ax, line = context
    x = np.linspace(0, 4 * np.pi, 500)
    y1 = np.sin(x + frame / 10)
    y2 = np.sin(1.2 * x - frame / 15)
    line.set_data(x, y1 + y2)

animator = Synimator(setup)
animator.animate(update, frames=200, fps=30)
animator.show()

API Reference

  • Synimator(setup): Creates the animator instance.

    • setup: Function returning (fig, ax, *artists).
  • animator.animate(update, frames, **kwargs): Builds the animation.

    • update: Function with signature update(frame, context).
    • frames: Frame count or iterable of frame values.
    • fps: Frames per second, default 16.
    • blit: Enable blitting for performance, default True.
    • embed: Jupyter embedding format ('jshtml' or 'video'), default 'jshtml'.
    • embed_limit: Maximum file size for embedding animations in notebooks (MB).
    • save_only: Skip display pre-rendering to optimize save performance in notebooks, default False.
  • animator.show(): Displays the animation according to the environment.

  • animator.save(filename, **kwargs): Saves animation to file.

    • filename: Output filename (e.g., animation.mp4).
    • **kwargs: Additional arguments for matplotlib's save method (e.g., dpi).

Additional Examples

Conway's Game of Life animation
import numpy as np
import matplotlib.pyplot as plt
from synimatic import Synimator

def setup():
    fig, ax = plt.subplots()
    grid = np.random.choice([0, 1], (30, 30), p=[0.8, 0.2])
    img = ax.imshow(grid, cmap='gray', vmin=0, vmax=1)
    ax.axis('off')
    update.grid = grid
    return fig, ax, img

def update(frame, context):
    fig, ax, img = context
    G = update.grid
    neighbors = sum(np.roll(np.roll(G, i, 0), j, 1)
                    for i in (-1, 0, 1) for j in (-1, 0, 1)
                    if (i != 0 or j != 0))
    G = ((neighbors == 3) | ((G == 1) & (neighbors == 2))).astype(int)
    update.grid = G
    img.set_data(G)

animator = Synimator(setup)
animator.animate(update, frames=100, fps=20)
animator.show()

# To save the animation:
# animator.save("game_of_life.mp4")
Double Pendulum
import numpy as np
import matplotlib.pyplot as plt
from synimatic import Synimator

def setup():
    fig, ax = plt.subplots()
    ax.set_xlim(-2, 2)
    ax.set_ylim(-2, 2)
    ax.set_aspect('equal')
    arm1, = ax.plot([], [], lw=2, color='red')
    arm2, = ax.plot([], [], lw=2, color='blue')
    trail, = ax.plot([], [], lw=1, color='green', alpha=0.5)
    update.trail_x, update.trail_y = [], []
    return fig, ax, arm1, arm2, trail

def update(frame, context):
    fig, ax, arm1, arm2, trail = context
    theta1 = np.pi/2 * np.cos(frame)
    theta2 = np.pi/2 * np.sin(2 * frame)
    x1, y1 = np.sin(theta1), -np.cos(theta1)
    x2, y2 = x1 + np.sin(theta2), y1 - np.cos(theta2)
    arm1.set_data([0, x1], [0, y1])
    arm2.set_data([x1, x2], [y1, y2])
    update.trail_x.append(x2)
    update.trail_y.append(y2)
    trail.set_data(update.trail_x[-100:], update.trail_y[-100:])

frames = np.linspace(0, 4 * np.pi, 500)
animator = Synimator(setup)
animator.animate(update, frames, fps=60)
animator.show()

Contributing

Contributions welcome. Report issues or submit pull requests at the GitHub repository.


License

Distributed under the MIT License.

Author

Jonathan AyalewGitHub

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

synimatic-1.0.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

synimatic-1.0.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file synimatic-1.0.0.tar.gz.

File metadata

  • Download URL: synimatic-1.0.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for synimatic-1.0.0.tar.gz
Algorithm Hash digest
SHA256 92070639d34ba79ab9888acbe53d4658875425c9a768f5a564ee2e559aa35f2b
MD5 1ec22e8a2338291bcdf3ffd326dd4c0e
BLAKE2b-256 1c2c85617356a77839c59e765543710fc3c25ecf67786e92e32c9635a55b343e

See more details on using hashes here.

File details

Details for the file synimatic-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: synimatic-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for synimatic-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b32a9ac9f19f4a932f6fecb148e152ddc09a8343caaabc0de00d66056f37354
MD5 a4055fccea6469cda9b79aa654f4beb9
BLAKE2b-256 1d0c5548ee11303ff2e05dafa6840f30ca641a69d62183f55f8acb424506b711

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