API Reference¶
pystreamgraph ¶
A compact Matplotlib streamgraph helper with:
- sorted or unsorted ordering, and optional global ordering helpers
- vertical margins (gaps) between streams
- optional value smoothing (moving-average)
- boundary curve smoothing with shape-preserving PCHIP splines (default)
- optional Catmull–Rom boundary curves
- flexible label placement: peak, start, end, max-width, balanced, sliding-window
- colormap selection via names, sequences, or label->color mappings
Author: Max Noichl & 🦖
catmull_rom_interpolate ¶
catmull_rom_interpolate(x, y, samples_per_seg=12)
Return dense x_s, y_s that pass through all control points using Catmull–Rom splines. samples_per_seg >= 1. When 1, each segment contributes one interior sample and the end point of the final segment is appended once.
moving_average ¶
moving_average(a, window)
Centered moving average with reflection at the edges. window must be odd and at least 1. When window <= 1 no smoothing is applied.
order_bestfirst ¶
order_bestfirst(X, Y)
Return (center_out_top, center_out_bottom) lists of layer indices. Greedy bipartition that adds the layer/side with lowest incremental L1 midline wiggle.
Reference: Di Bartolomeo & Hu (2016), BestFirst stage.
order_twoopt ¶
order_twoopt(X, Y, repeats=8, scans=3, rng=None)
Build BestFirst bipartition, then improve each side with inside-out scans, repeating from shuffled starts to avoid bad bipartitions. Returns a global order.
Reference: Di Bartolomeo & Hu (2016), TwoOpt stage.
pchip_interpolate ¶
pchip_interpolate(x, y, samples_per_seg=12)
Return dense (xp, yp) sampled using a shape-preserving cubic (PCHIP).
- Uses SciPy's PchipInterpolator when available for numerical robustness.
- Falls back to a pure NumPy Fritsch–Carlson implementation otherwise.
place_label_fast_on_layer ¶
place_label_fast_on_layer(ax, Xp, bottom, top, text, min_fontsize=6, max_fontsize=18, fontfamily=None, fontweight='bold', shrink=0.9, color='black', sigma_override=None)
Try to place text
inside the stream using the paper's O(m log W) scheme.
Returns True if placed.
plot_streamgraph ¶
plot_streamgraph(X, Y, labels=None, sorted_streams=False, margin_frac=0.08, smooth_window=1, cmap=None, linewidth=0.0, alpha=1.0, label_placement=True, label_position='balanced', label_color=None, label_fontsize=10, label_weight='bold', curve_samples=200, curve_method='pchip', baseline='center', wiggle_reduction='weighted', global_order=None, pad_frac=0.05, label_min_gap_frac=0.02, label_edge_offset_frac=0.02, label_connectors=False, label_connector_color=None, label_connector_alpha=0.6, label_connector_linewidth=0.8, ax=None, label_kwargs=None, label_anchor='center', label_plot_anchors=False, label_point_kwargs=None, label_balanced_inset_frac=0.05, label_fontsize_min=None, label_fontsize_max=None, label_fontsize_by='sum', label_balanced_progress=False, label_balanced_fit_tolerance_px=2.0, label_balanced_debug_segments=False, label_balanced_debug_kwargs=None, label_balanced_candidates_per_layer=60, label_balanced_restarts=2, label_balanced_T0=2.5, label_balanced_T_min=0.0005, label_balanced_alpha=0.94, label_balanced_iters_per_T=240, label_balanced_min_thickness_q=0.5)
Plot a configurable streamgraph.
Parameters: |
|
---|
Returns: |
|
---|
Notes
Y
must be non-negative.- If
global_order
is provided, it is used as a fixed order; otherwise the order is either the input order (sorted_streams=False
) or sorted by value at each time step (sorted_streams=True
). - Boundary curve interpolation is applied to both bottom and top envelopes per layer after stacking on the original grid.
Examples:
>>> import numpy as np
>>> X = np.arange(50)
>>> rng = np.random.default_rng(0)
>>> Y = np.clip(rng.normal(0.6, 0.2, size=(5, 50)), 0, None)
>>> ax = plot_streamgraph(X, Y, labels=list("ABCDE"), label_position='balanced')
smooth_series ¶
smooth_series(Y, window=1)
Apply moving average along time for each row of Y. Returns new array.
streamgraph_envelopes ¶
streamgraph_envelopes(Y, margin_frac=0.0, order_mode='by_value', X=None, wiggle_reduction='none', global_order=None)
Compute bottom and top envelopes for each layer.
Parameters: |
|
---|
Returns: |
|
---|