Convert SciPy linkage matrices to plot-ready DataFrames — used for ggplot/Plotnine dendrograms.
Project description
linkage2df
Easily plot SciPy dendrograms using the Grammar of Graphics in Python
linkage2df is a tool that converts SciPy linkage matrices (typically used for hierarchical clustering) into tidy DataFrames. By transforming the dendrogram tree into a set of segments with (x, y) coordinates, you can leverage the full power of Plotnine (or any ggplot2 clone) to create highly customizable dendrograms.
Features
- Tidy Data Output: Converts hierarchical trees into a simple DataFrame of segments with
x_start,y_start,x_end, andy_endcolumns. - Cluster Propagation: Automatically assigns clusters to higher-level branches based on their children, allowing for intuitive coloring of the tree.
- Plotnine Ready: Designed specifically to work with
geom_segment()in the Grammar of Graphics.
Installation
You can install the package directly from your local directory:
pip install .
Quick start
import numpy as np
import pandas as pd
from scipy.cluster.hierarchy import linkage, fcluster
from plotnine import ggplot, aes, geom_segment, theme_minimal
from linkage2df import linkage_to_segments
# 1. Generate some dummy data
np.random.seed(42)
X = np.random.rand(15, 4)
# 2. Compute linkage matrix & clusters
linkage_matrix = linkage(X, method="ward")
clusters = fcluster(linkage_matrix, t=0.7, criterion='distance')
# 3. Convert linkage matrix to a tidy segments DataFrame
df = linkage_to_segments(linkage_matrix, clusters=clusters)
# 4. Plot using Plotnine
p = (
ggplot(df) +
geom_segment(aes(x='x_start', y='y_start', xend='x_end', yend='y_end'), size=0.75) +
theme_minimal()
)
print(p)
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 linkage2df-1.0.0.tar.gz.
File metadata
- Download URL: linkage2df-1.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.13.2 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65e90f80291deea2225d5e4bf06c70b9b92f5e5fac6dfa57921a8d99e5b13144
|
|
| MD5 |
204de5f1ddb511d10c49aa84d519295d
|
|
| BLAKE2b-256 |
590fe985ce98fda33eb92e57cda924c4984fd7ec48c2ab03bd827d681630ab78
|
File details
Details for the file linkage2df-1.0.0-py3-none-any.whl.
File metadata
- Download URL: linkage2df-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.13.2 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7fa7b7facc7c708acff2c5f77d72e3237830a8dfdf7380078596354415da497
|
|
| MD5 |
bd5ceb6fc262aa0dc805645c37255601
|
|
| BLAKE2b-256 |
d83f13e278a6adbef0fd4a746967c6d179f880dbc1b995709a5f6c31a4aaea24
|