Skip to main content

Draw text along an arbitrary curve in matplotlib, with arc-length positioning and a perpendicular offset.

Project description

curved-text

CI

Draw text that follows an arbitrary curve in matplotlib.

Direct labeling versus a legend

Label curves along their own paths instead of in a legend, so the eye never leaves the data to decode a colour key.

Each character is placed in display coordinates and rotated to the local tangent of the curve, recomputed on every draw, so the label keeps following the curve through layout, resizing, and interactive panning or zooming. Placement is controlled by three independent parameters:

  • pos -- where the label is anchored along the curve, as a fraction of arc length (0.0 = first point, 1.0 = last).
  • anchor -- which part of the label lands at pos: "start", "center", or "end".
  • offset -- a perpendicular shift off the curve, in typographic points, along the normal of the label's chord (positive is above a left-to-right curve).

A label that overruns either end of the curve is not clipped: the curve is extended along its end tangent and the overrunning glyphs sit on that straight extension.

Install

pip install curved-text

Or, from a clone, an editable install:

pip install -e .

Usage

import numpy as np
import matplotlib.pyplot as plt
from curved_text import curved_text

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
curved_text(ax, x, y, "text that follows the curve",
            pos=0.5, anchor="center", offset=6.0, color="C3")
plt.show()

A label following a sine wave

More worked examples -- the three placement controls, overrun behaviour, and integration with seaborn and pandas -- are in examples/.

The object-oriented form is also available:

from curved_text import CurvedText

CurvedText(x, y, "along the curve", ax, pos=0.2, anchor="start", offset=4.0)

Note the axes argument position: the CurvedText class takes it after x, y, text (matching matplotlib.text.Text), while the curved_text function takes it first (matching matplotlib's axes-first helper functions).

Any extra keyword arguments (color, fontsize, alpha, fontfamily, ...) are passed through to each character's matplotlib.text.Text.

Works with seaborn, pandas, and other matplotlib-backed libraries

curved_text needs a matplotlib.axes.Axes, so it works with any library that draws on matplotlib. seaborn's axes-level functions return an Axes, its figure-level functions expose one through .axes, and pandas DataFrame.plot returns an Axes as well. Pass that axes in directly:

import seaborn as sns

ax = sns.lineplot(data=df, x="x", y="y")
curved_text(ax, df["x"], df["y"], "along the curve",
            pos=0.5, anchor="center", offset=6.0)

Notes

  • The curve (x, y) should be ordered along the curve (monotonic in arc length) and have at least two points.
  • Arc length and the offset are computed in display space, so spacing and the offset are correct at any DPI and figure size.

License

MIT

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

curved_text-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

curved_text-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for curved_text-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5797a3c8352ce95e32b09dd07783af4ac2bda3cbe58cd7c4ca2ea556ce8df3f5
MD5 4495e9c3ec81fba77099a915e6821464
BLAKE2b-256 f88a775d5db4d93fd0e60dd546cb9d20e54ca062484edb1d0373a3c4948b41e2

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on thiebes/curved-text

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

File details

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

File metadata

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

File hashes

Hashes for curved_text-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb8fdaf70c1da911715d0f48bfa8e1d8a6250ae764fdae3a68d5da9677971065
MD5 81ff40758634467f059347a94f1593b1
BLAKE2b-256 63a8717b8a2f400f99ba04f0ab434814e4450bf63a344b8232d9304e8d314f6a

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on thiebes/curved-text

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