Matplotlib helper library for plotting wrapped, angular, or periodic data.
Project description
mpl_wrap
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.
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()
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:
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8fed27e29246d4633dddf6c677882bf3b967ecc168b9ef767de51dc3e495e6f
|
|
| MD5 |
9ffcedb3e775d07944ee13d43a1dd747
|
|
| BLAKE2b-256 |
1152e10bc009dffe4a4e15148d7ce876426b0ea470b15372bc7b234fbe983582
|
Provenance
The following attestation bundles were made for mpl_wrap-0.1.0.tar.gz:
Publisher:
publish.yml on scottshambaugh/mpl_wrap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mpl_wrap-0.1.0.tar.gz -
Subject digest:
b8fed27e29246d4633dddf6c677882bf3b967ecc168b9ef767de51dc3e495e6f - Sigstore transparency entry: 2194930618
- Sigstore integration time:
-
Permalink:
scottshambaugh/mpl_wrap@32af868739444c706f8ab2d25b7c9b2a6451bf18 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/scottshambaugh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@32af868739444c706f8ab2d25b7c9b2a6451bf18 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
200f8acb6f47d7a28717d29d0051d48d6c2cc5ba6d185a07b9bf1bc77a917ec7
|
|
| MD5 |
dc1f9ca9a8e50cbbf2a337be73b2c3df
|
|
| BLAKE2b-256 |
e6ca182e381f476926565fabf17aa69b9d03b3492c280f3bb2c39e26871e4891
|
Provenance
The following attestation bundles were made for mpl_wrap-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on scottshambaugh/mpl_wrap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mpl_wrap-0.1.0-py3-none-any.whl -
Subject digest:
200f8acb6f47d7a28717d29d0051d48d6c2cc5ba6d185a07b9bf1bc77a917ec7 - Sigstore transparency entry: 2194930621
- Sigstore integration time:
-
Permalink:
scottshambaugh/mpl_wrap@32af868739444c706f8ab2d25b7c9b2a6451bf18 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/scottshambaugh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@32af868739444c706f8ab2d25b7c9b2a6451bf18 -
Trigger Event:
release
-
Statement type: