Skip to main content

hilbertplot

PyPI version CI Python versions License: MIT

Plot long numeric sequences on any of 40 two-dimensional Hilbert-type curves. Choose a curve by number, label, or paper symbol. The interface is functions returning ordinary arrays, dictionaries, lists, and Matplotlib figures; custom classes stay internal.

Top-level completion has just six entries: plot, plot_curve, gallery, curves, data, and config. Specialist functions live under those three namespaces: hp.curves for the catalog and geometry, hp.data for arrays, and hp.config for optional cache and allocation controls.

Install

pip install "hilbertplot[plot]"   # omit [plot] for numpy-only operations

Plot your data

import numpy as np
import matplotlib.pyplot as plt
import hilbertplot as hp

data = np.sin(np.arange(1000) / 10)
fig = hp.plot(data)                       # standard Hilbert curve
fig = hp.plot(data, curve=12)             # any curve index 0..39
fig = hp.plot(data, curve="Moore")        # or a label; "12H" also works
fig.savefig("data.png", dpi=200)
plt.show()                               # display figures in a script

Each call makes one plot and returns a Matplotlib Figure. No window is opened implicitly. Use ax= to draw on your own Matplotlib axes. Figures remain open until you close them with plt.close(fig).

Fourier, difference, and granularity maps

fig = hp.plot(data, fourier=True)          # centred 2-D Fourier magnitude
fig = hp.plot(data, difference=True)       # full locality-loss field
fig = hp.plot(data, granularity=16)        # average consecutive blocks of 16
fig = hp.plot(data, fourier=True, granularity=16, window=True)

fourier and difference are boolean flags, both off by default (False / 0). They are mutually exclusive: setting both raises ValueError.

  • Fourier: transforms the mapped 2-D image, not the original 1-D signal. Defaults to log1p(abs(FFT)) with the mean subtracted. Use log=False for raw magnitude, remove_dc=False to retain the mean, or window=True for a Hann window.
  • Difference: displays the mean absolute visit-index distance to each cell's up-to-eight neighbours. It describes the curve's geometry, independently of data values.
  • Granularity: replaces each consecutive sequence block by copies of its mean. The sequence and image retain their sizes; this is not image downsampling. It affects the data and its Fourier transform. Geometry-based difference maps stay unchanged.

Draw a curve

fig = hp.plot_curve(12, order=4)
fig = hp.plot_curve("Moore", order=4, color="steelblue")
fig = hp.plot_curve(0, order=8, mode="fill")

# Arbitrary square and quasisquare: draw the path itself, with no data.
fig = hp.plot_curve(3, shape=(15, 15), mode="line")
fig = hp.plot_curve(3, shape=(15, 16), mode="line")
fig = hp.plot_curve(3, shape=(16, 15), mode="line")

order=k visits 4**k cells in a 2**k square. Curve drawing automatically switches from coloured lines to a filled visit-order image at order 8. Explicit mode="line" or "fill", linewidth, and endpoint markers are also available.

shape=(rows, columns) selects explicit dimensions instead of an order. A quasisquare's sides differ by one. Arbitrary squares and quasisquares require one of curves 0, 3, 4, 5, 24, 29, 30, 31. Quasisquare orientation is selected automatically to keep every step connected. Use either shape or order, not both. hp.curves.grid((rows, columns), curve=...) returns the same path as coordinates.

Colour and fitting

from matplotlib.colors import LogNorm

fig = hp.plot(np.arange(1, 1001), norm=LogNorm(), colorbar=True)
fig = hp.plot(data, cmap=["navy", "white", "firebrick"], vmin=-1, vmax=1)
compact = hp.data.to_grid(data, fit="square")      # supported curves only

hp.plot() and hp.data.to_grid() share these layout choices:

fit Behaviour
"pad" (default) Smallest enclosing power-of-two square; works with every curve
"square" Tight ceil(sqrt(N)) square; only the eight generalizing curves
"auto" Tight square where supported, otherwise power-of-two padding
"truncate" Largest power-of-two square inside the data; explicitly discards the tail

Padding is NaN and appears transparent. order= fixes the grid; if it is too small, the call raises unless you explicitly choose fit="truncate". Pass either norm= or vmin/vmax, not both. Extra Matplotlib image options such as alpha are supported.

Arrays and index maths (no Matplotlib needed)

img = hp.data.to_grid(data, curve=12)
spectrum = hp.data.fourier(data, curve=12, window=True)
recovered = hp.data.from_grid(img, curve=12, length=len(data))
labels = hp.curves.label_map(12, side=img.shape[0])
differences = hp.curves.difference_map(12, side=img.shape[0])

pts = hp.curves.points(12, order=4)              # (256, 2) array of (x, y) cells
word = hp.curves.word(12, order=4)               # u/d/l/r strokes
arbitrary = hp.curves.grid(100, curve=0)         # arbitrary square, supported curves only
xy = hp.curves.index_to_xy(10**18, 40, curve=12)      # O(order), no full-grid allocation
assert hp.curves.xy_to_index(*xy, 40, curve=12) == 10**18
assert hp.curves.is_space_filling(pts, 16)
key = hp.curves.canonical_form(pts)             # compare paths up to symmetry/reversal

Images are indexed [y, x]; coordinates are (x, y). Plotting and Fourier maps accept real, nonempty scalar sequences; choose magnitude, phase, real or imaginary values explicitly for complex data. Plotting rejects infinities. NaNs propagate through granularity block means; Fourier maps mean-fill missing cells and require at least one finite value.

Array conversion is separate from display: to_grid() / from_grid() preserve numeric dtypes, complex values, large integers, and masks. With no padding or averaging, conversion is exact. Default NaN padding keeps floating/complex dtypes; integer and boolean padding uses a NumPy masked array to retain precision. Use fill=0 for ordinary integer padding. length= explicitly selects a prefix without deleting legitimate NaNs or masked values.

# A square RGB image becomes a truly 1-D sequence. Each pixel's channels
# stay consecutive; use the full original shape when reconstructing.
rgb = np.arange(16 * 16 * 3, dtype=np.uint16).reshape(16, 16, 3)
sequence = hp.data.from_grid(rgb, curve=12)
assert sequence.ndim == 1 and sequence.size == rgb.size
restored = hp.data.to_grid(sequence, curve=12, shape=rgb.shape)
np.testing.assert_array_equal(restored, rgb)
assert restored.dtype == rgb.dtype

# The same functions use exact quasisquare paths for supported curves.
square_or_quasisquare = np.arange(15 * 16).reshape(15, 16)
sequence = hp.data.from_grid(square_or_quasisquare, curve=3)
restored = hp.data.to_grid(sequence, curve=3, shape=square_or_quasisquare.shape)
np.testing.assert_array_equal(restored, square_or_quasisquare)

Conversion uses the same connected path as plot_curve(shape=...). Spatial shapes must be supported squares or quasisquares; general rectangles are not supported. Unsupported shapes raise an error without padding or cropping them into a different traversal. shape= specifies the output's original dimensions; it cannot be combined with order=. Converting a grid with another curve gives a reversible reordering; keep all entries, including padding, when reversing it.

Discover curves

info = hp.curves.info(12)                       # ordinary metadata dictionary
print(info["name"], info["index"], info["kernels"])
print([(c["index"], c["name"]) for c in hp.curves.catalog()])
print([c["index"] for c in hp.curves.generalizing()])
fig = hp.gallery(hp.curves.by_kernels(3, 5), order=3)

Under hp.curves, catalog() lists all 40 curves: 6 proper(), 6 improper() (together 12 homogeneous()), and 28 inhomogeneous(). Groups return ordinary lists of metadata dictionaries. closed() lists the four loops; generalizing() lists curves 0, 3, 4, 5, 24, 29, 30, and 31, which support arbitrary square sizes.

Under hp.config, clear_cache() releases cached generation data, and cache_info() reports its use. cell_limit() / set_cell_limit(n) control the allocation guard. Grid size grows exponentially with order; the default limit is a ceiling, not a memory reservation.

More runnable examples are in examples/. The curve classification follows Estevez-Rams et al., Hilbert curves in two dimensions, Rev. Cub. Fís. 34, 9 (2017). The arbitrary-square research is in research/quasisquares/.

Try all the features

The complete runnable example draws separate figures for the curve, data, coarse-grained data, Fourier and difference maps, colour options, fitting modes, and a gallery. It also checks numeric round trips, geometry, indexing and groups.

python examples/try_api.py
# Save figures and run all checks without opening windows:
MPLBACKEND=Agg python examples/try_api.py --save-only

Figures are saved in examples/output/try_api/.

Local API migration

This working-tree redesign replaces the object-based 0.2.0 interface:

Previous New
hilbert_plot(12, data).draw() plot(data, curve=12)
curve(12).draw(4) plot_curve(12, order=4)
plot.save(path) fig = plot(data); fig.savefig(path)
curve(12).points(4) hp.curves.points(12, order=4)
curve(12).unroll(img) hp.data.from_grid(img, curve=12)
curve(12).name hp.curves.info(12)["name"]
proper().names [c["name"] for c in hp.curves.proper()]

The old Curve, CurveList, HilbertPlot, and hilbert_plot exports are removed. difference=True now renders the complete difference map. Rendering defaults to power-of-two padding. These are intentional interface changes under local review.

License

MIT © Daniel Estevez — LICENSE.

Metadata

Release files for hilbertplot 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hilbertplot 1.0.1
File Size Uploaded Provenance Actions
hilbertplot-1.0.1.tar.gz 89.9 kB With provenance Details for hilbertplot-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hilbertplot 1.0.1
File Interpreter ABI Platform Actions
hilbertplot-1.0.1-py3-none-any.whl Python 3 none any Details for hilbertplot-1.0.1-py3-none-any.whl

Total release size: 154.7 kB

Release files / hilbertplot-1.0.1.tar.gz

Download URL hilbertplot-1.0.1.tar.gz
Size 89.9 kB
Tags Source
SHA-256 checksum
How to use checksums
10dbc45ee54904527a9b82763c66d64e9bf327eab98af01c74512cbc586ae333
BLAKE2b-256 checksum
How to use checksums
14860083a5aa24c874f21abaae891beb90193abbab796451f6d7fc400f4cac13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 1, 2026.

Transparency log

Release files / hilbertplot-1.0.1-py3-none-any.whl

Download URL hilbertplot-1.0.1-py3-none-any.whl
Size 64.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6cf8a95bddd46da26aa3fd6ce0d677c02c32481dc51237741b5ca2d13c9bdaf
BLAKE2b-256 checksum
How to use checksums
5c749b0ae93c85bbb0fb5fa869163eba7199613eeac34de43aeba4d3f662236e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 1, 2026.

Transparency log

Security for hilbertplot 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for hilbertplot-1.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page