Skip to main content

Matplotlib helper library for plotting wrapped, angular, or periodic data.

Project description

mpl_wrap

Builds Tests

Matplotlib helper functions for plotting wrapped, angular, or periodic data: angles, phases, times of day, longitudes, and anything else that repeats or rotates.

Manually plotting this data can be tricky. Using a modulus such as y % 360 is simple, but introduces a few problems:

  • Line jumps at the crossing points, and lines that stop short of the wrap boundaries at the crossing points
  • Aliasing when data spans multiple crossings, obscuring the real underlying behavior
  • Completely broken rendering for fill_between

mpl_wrap solves these issues, and provides simple functions to make plotting wrapped data easy.

Unwrapped vs modulus vs mpl_wrap

Installation

pip install mpl_wrap

Or install from source:

git clone https://github.com/scottshambaugh/mpl_wrap.git
cd mpl_wrap
uv sync --group dev

Basic Usage

import numpy as np
import matplotlib.pyplot as plt
from mpl_wrap import set_wrap, plot_wrapped, fill_between_wrapped, errorbar_wrapped

t = np.linspace(0, 10, 500)
angle = 80.0 * t  # degrees
width = 5.0 + 4.0 * t  # degrees

fig, ax = plt.subplots()
set_wrap(ax, wrapy=(0, 360))  # helpers on ax now wrap y into (0, 360)
fill_between_wrapped(ax, t, angle - width, angle + width, alpha=0.3, label='uncertainty')
plot_wrapped(ax, t, angle, label='angle')
ax.set(xlabel="time (s)", ylabel="angle (deg)")
ax.legend()

Basic usage: wrapped angle with uncertainty band and error bars

The helpers mirror their matplotlib counterparts, taking the target Axes as the first argument plus optional wrapx / wrapy (min, max) windows:

mpl_wrap mirrors
plot_wrapped(ax, x, y, ...) ax.plot
scatter_wrapped(ax, x, y, ...) ax.scatter
fill_between_wrapped(ax, x, y1, y2) ax.fill_between
stairs_wrapped(ax, values, edges) ax.stairs
errorbar_wrapped(ax, x, y, yerr, xerr) ax.errorbar

Passing wrapx=False / wrapy=False disables wrapping for a single call (or clears the stored window when passed to set_wrap), and wrapx=True / wrapy=True requires the stored window. set_wrap also sets the axis limits to the window by default. Opt out with set_lims=False, or pass seam_lines=True to mark the window edges with lines.

You must pass the original unwrapped data for these to work. If your data is already wrapped, np.unwrap may be able to recover that if it's sampled at a high enough rate.

API

The helpers are free functions, and are also available as methods on an AxesWrap axes. These three are equivalent:

from mpl_wrap import set_wrap, plot_wrapped, wrap_axes

# 1. Free functions, on any existing axes
fig, ax = plt.subplots()
set_wrap(ax, wrapy=(0, 360))
plot_wrapped(ax, t, angle)

# 2. Methods on an AxesWrap axes, created with the "wrap" projection
fig, ax = plt.subplots(subplot_kw={"projection": "wrap"})
ax.set_wrap(wrapy=(0, 360))
ax.plot_wrapped(t, angle)

# 3. Methods on an existing axes, upgraded in place from Axes to AxesWrap
fig, ax = plt.subplots()
wrap_axes(ax, wrapy=(0, 360))
ax.plot_wrapped(t, angle)

The data processing is also exposed on its own: wrap_line and wrap_points take data plus windows and return the wrapped arrays without plotting anything (also available as AxesWrap methods).

Wrapping x, y, or both

Both axes can be wrapped independently or together:

Circle wrapped in x, y, and both

Datetime axes

Datetime data and windows work on either axis. Here a five-day series is wrapped to show a time-of-day view:

set_wrap(ax, wrapx=(t0, t0 + np.timedelta64(1, "D")))
plot_wrapped(ax, times, signal)

Datetime wrapping

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

mpl_wrap-0.1.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

mpl_wrap-0.1.0-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file mpl_wrap-0.1.0.tar.gz.

File metadata

  • Download URL: mpl_wrap-0.1.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for mpl_wrap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b8fed27e29246d4633dddf6c677882bf3b967ecc168b9ef767de51dc3e495e6f
MD5 9ffcedb3e775d07944ee13d43a1dd747
BLAKE2b-256 1152e10bc009dffe4a4e15148d7ce876426b0ea470b15372bc7b234fbe983582

See more details on using hashes here.

Provenance

The following attestation bundles were made for mpl_wrap-0.1.0.tar.gz:

Publisher: publish.yml on scottshambaugh/mpl_wrap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mpl_wrap-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mpl_wrap-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for mpl_wrap-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 200f8acb6f47d7a28717d29d0051d48d6c2cc5ba6d185a07b9bf1bc77a917ec7
MD5 dc1f9ca9a8e50cbbf2a337be73b2c3df
BLAKE2b-256 e6ca182e381f476926565fabf17aa69b9d03b3492c280f3bb2c39e26871e4891

See more details on using hashes here.

Provenance

The following attestation bundles were made for mpl_wrap-0.1.0-py3-none-any.whl:

Publisher: publish.yml on scottshambaugh/mpl_wrap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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