Skip to main content

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.

Penguin Dendrogram

Features

  • Tidy Data Output: Converts hierarchical trees into a simple DataFrame of segments with x_start, y_start, x_end, and y_end columns.
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linkage2df-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

linkage2df-1.0.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

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

Hashes for linkage2df-1.0.0.tar.gz
Algorithm Hash digest
SHA256 65e90f80291deea2225d5e4bf06c70b9b92f5e5fac6dfa57921a8d99e5b13144
MD5 204de5f1ddb511d10c49aa84d519295d
BLAKE2b-256 590fe985ce98fda33eb92e57cda924c4984fd7ec48c2ab03bd827d681630ab78

See more details on using hashes here.

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

Hashes for linkage2df-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7fa7b7facc7c708acff2c5f77d72e3237830a8dfdf7380078596354415da497
MD5 bd5ceb6fc262aa0dc805645c37255601
BLAKE2b-256 d83f13e278a6adbef0fd4a746967c6d179f880dbc1b995709a5f6c31a4aaea24

See more details on using hashes here.

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