Color arbitrary SVG paths by data values — turn any SVG into a heatmap
Project description
pathy-svg
Color arbitrary SVG paths by data values — turn any SVG into a heatmap.
Installation
pip install pathy-svg
Optional extras:
pip install pathy-svg[export] # PNG, PDF, JPEG export (cairosvg + Pillow)
pip install pathy-svg[full] # All features including Jupyter display
Quick Start
from pathy_svg import SVGDocument
doc = SVGDocument.from_file("examples/map.svg")
data = {
"stomach": 0.5,
"liver": 0.8,
"heart": 0.3,
"lung_l": 0.6,
"lung_r": 0.7,
}
doc.heatmap(data, palette="YlOrRd").legend(title="Expression").save("output.svg")
Gradient and Pattern Fills
from pathy_svg import SVGDocument, GradientSpec
doc = SVGDocument.from_file("examples/map.svg")
# Gradient fill
doc.gradient_fill({
"stomach": GradientSpec(start="#ff0000", end="#0000ff", direction="horizontal"),
}).save("gradient.svg")
# Pattern fill (string shorthand or PatternSpec)
doc.pattern_fill({
"liver": "crosshatch",
"heart": "dots",
}).save("patterned.svg")
Stroke Mapping and Highlighting
# Map data to stroke width and color
doc.stroke_map(data, width_range=(1, 5), palette="Reds").save("strokes.svg")
# Highlight specific elements, dim the rest
doc.highlight(["stomach", "liver"]).save("highlighted.svg")
Matching by Data Attributes
# Match elements by data-region instead of id
doc.heatmap({"north": 0.8, "south": 0.3}, key_attr="data-region").save("regions.svg")
# Works with all methods: recolor, stroke_map, highlight, annotate, etc.
doc.recolor({"north": "#ff0000"}, key_attr="data-region").save("recolored.svg")
Group Aggregation and Layers
# Color groups by the mean of their children's values
doc.heatmap_groups(data, agg="mean", palette="YlOrRd").save("groups.svg")
# Or use a custom aggregation function
doc.heatmap_groups(data, agg=lambda vals: max(vals) - min(vals)).save("range.svg")
# Compose multiple visualization layers
result = (
doc.layers()
.add("heat", lambda d: d.heatmap(data, palette="YlOrRd"))
.add("borders", lambda d: d.stroke_map(data, palette="Greys"))
.add("labels", lambda d: d.annotate({"stomach": "S", "liver": "L"}))
.flatten()
)
result.save("layered.svg")
The source distribution includes a runnable examples/ directory with:
examples/map.svgexamples/data.csvexamples/baseline.csvexamples/treatment.csv
Features
- Heatmaps — data-driven coloring with any matplotlib colormap
- Categorical coloring — map categories to distinct colors
- Manual recolor — direct ID-to-color mapping
- Gradient fills — apply linear gradients (horizontal, vertical, diagonal) to elements
- Pattern fills — hatching, crosshatch, dots, and custom SVG patterns for accessibility
- Stroke mapping — map data to stroke width and/or color independently of fill
- Highlight/dim — emphasize specific elements while dimming others with desaturation
- Group aggregation — color
<g>elements by aggregating children (mean, sum, min, max, median, or custom callable) - Multi-layer system — compose named visualization layers with show/hide/reorder
- Diff visualization — compare datasets with delta, ratio, log2ratio, or percent change modes
- Side-by-side comparison — multiple datasets in a single SVG
- Legends — gradient, discrete, and categorical legend types
- Annotations — text labels at element centroids or custom positions
- Tooltips — hover text via SVG
<title>or CSS popups - Animations — CSS keyframe effects (pulse, fade_in, blink, sequential)
- Export — PNG, PDF, JPEG via cairosvg and Pillow
- Jupyter — inline SVG display with
_repr_svg_and_repr_mimebundle_ - CLI — heatmap, inspect, validate, guide, diff, and export commands
- Flexible element matching — match elements by
id,data-*attributes, orclassviakey_attr - Immutable API — method chaining with new instances returned on each call
- DataFrame support — load data directly from pandas
- Theme presets — medical, geographic, heatmap_classic
CLI Usage
# Create a heatmap
pathy-svg heatmap examples/map.svg examples/data.csv --id-col organ --value-col expression --palette YlOrRd --legend -o out.svg
# Inspect SVG structure
pathy-svg inspect examples/map.svg
# Validate data IDs against SVG
pathy-svg validate examples/map.svg examples/data.csv --id-col organ
# Compare two datasets
pathy-svg diff examples/map.svg examples/baseline.csv examples/treatment.csv --id-col organ --value-col expression --mode delta -o diff.svg
# Export to PNG
pathy-svg export examples/map.svg -o map.png --width 1200
API Overview
Loading
| Method | Description |
|---|---|
SVGDocument.from_file(path) |
Load from file path |
SVGDocument.from_string(svg) |
Load from SVG string |
SVGDocument.from_url(url) |
Load from URL |
Coloring
| Method | Description |
|---|---|
.heatmap(data, palette=...) |
Apply data-driven coloring |
.heatmap_from_dataframe(df, ...) |
Heatmap from pandas DataFrame |
.recolor(color_map) |
Manual ID-to-color mapping |
.recolor_by_category(category_map) |
Categorical coloring |
.gradient_fill(gradients) |
Apply linear gradients to elements |
.pattern_fill(patterns) |
Apply hatching, dots, or custom patterns |
.stroke_map(data, width_range=..., palette=...) |
Map data to stroke width/color |
.highlight(ids) |
Emphasize elements, dim the rest |
.heatmap_groups(data, agg=...) |
Color groups by aggregating children |
Layers
| Method | Description |
|---|---|
.layers() |
Create a LayerManager for composing layers |
LayerManager.add(name, fn) |
Add a named layer |
LayerManager.hide(name) / .show(name) |
Toggle layer visibility |
LayerManager.reorder(names) |
Change layer order |
LayerManager.flatten() |
Render all visible layers to an SVGDocument |
Visualization
| Method | Description |
|---|---|
.legend(title=..., position=...) |
Add a legend |
.diff(baseline, treatment, mode=...) |
Diff two datasets |
.compare(datasets, layout=...) |
Side-by-side comparison |
.annotate(labels) |
Add text labels |
.add_tooltips(texts) |
Add hover tooltips |
.animate(effect=..., duration=..., loop=...) |
CSS animations |
Inspection
| Method | Description |
|---|---|
.path_ids |
List of all path element IDs |
.group_ids |
List of all group element IDs |
.element_ids |
List of all element IDs |
.viewbox |
SVG viewBox as ViewBox namedtuple |
.dimensions |
(width, height) tuple |
.inspect_paths() |
Detailed metadata for all colorable elements |
.validate_ids(ids) |
Check which IDs match SVG elements |
Export
| Method | Description |
|---|---|
.save(path) |
Write SVG to file |
.to_string() |
SVG as string |
.to_bytes() |
SVG as bytes |
.to_png(path) |
Export to PNG |
.to_pdf(path) |
Export to PDF |
.to_jpeg(path) |
Export to JPEG |
.show() |
Display in Jupyter |
License
This project is licensed under the GNU General Public License v3.0 — see the LICENSE file for details.
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
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 pathy_svg-0.3.1.tar.gz.
File metadata
- Download URL: pathy_svg-0.3.1.tar.gz
- Upload date:
- Size: 242.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8ab4c09e3ef3d9d09c43f1cf469efdddb67fc157e5d883a471cae4197c242a7
|
|
| MD5 |
8fba4f09c756876e6c11bf2fafe9233c
|
|
| BLAKE2b-256 |
b9fc7480fce42532037be10f43c1ae4d086c8f248aa63453a10a0d75d0d84668
|
Provenance
The following attestation bundles were made for pathy_svg-0.3.1.tar.gz:
Publisher:
publish.yml on yigityargili991/pathy_svg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pathy_svg-0.3.1.tar.gz -
Subject digest:
f8ab4c09e3ef3d9d09c43f1cf469efdddb67fc157e5d883a471cae4197c242a7 - Sigstore transparency entry: 2192722352
- Sigstore integration time:
-
Permalink:
yigityargili991/pathy_svg@f3624cfd0eaeee391c85cac08cd18ad3def158ab -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/yigityargili991
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f3624cfd0eaeee391c85cac08cd18ad3def158ab -
Trigger Event:
release
-
Statement type:
File details
Details for the file pathy_svg-0.3.1-py3-none-any.whl.
File metadata
- Download URL: pathy_svg-0.3.1-py3-none-any.whl
- Upload date:
- Size: 73.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5fd8ba636735158b31ad10d604834f87e27c6b654a6eb91c921dd2fb0546e19
|
|
| MD5 |
d07d9a1a9537e0d493311bbf39766e40
|
|
| BLAKE2b-256 |
3add2547cb69e3e346779f655d351d1681b713e6b80a4023302539469ffd10b0
|
Provenance
The following attestation bundles were made for pathy_svg-0.3.1-py3-none-any.whl:
Publisher:
publish.yml on yigityargili991/pathy_svg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pathy_svg-0.3.1-py3-none-any.whl -
Subject digest:
c5fd8ba636735158b31ad10d604834f87e27c6b654a6eb91c921dd2fb0546e19 - Sigstore transparency entry: 2192722553
- Sigstore integration time:
-
Permalink:
yigityargili991/pathy_svg@f3624cfd0eaeee391c85cac08cd18ad3def158ab -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/yigityargili991
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f3624cfd0eaeee391c85cac08cd18ad3def158ab -
Trigger Event:
release
-
Statement type: