Skip to content

robert-mcdermott/treemap-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Treemap Generator

A Python utility for creating treemap visualizations from CSV data files. Generates both PNG and SVG output files with customizable labels, values, and formatting.

example

Features

  • Generate treemaps from CSV data
  • Customizable column mapping for labels and values
  • Optional value prefix (e.g., "$" for monetary values)
  • Output in both PNG and SVG formats
  • Clean, professional visualization with formatted numbers

Requirements

  • Python 3.x
  • uv (package manager)
  • Dependencies: pandas, plotly

Installation

Install dependencies using uv:

uv pip install pandas plotly kaleido

Usage

Basic Syntax

uv run treemap-generator.py <csv_file> <title> [options]

Arguments

  • csv_file - Path to your CSV data file (required)
  • title - Title for the treemap (required)

Options

  • -l, --label-col - Column name for labels (default: 'Item')
  • -v, --value-col - Column name for values (default: 'Total')
  • -o, --output - Output file prefix (default: 'treemap')
  • -p, --prefix - Optional prefix for values (e.g., "$" for money)

Examples

Basic usage with default columns

uv run treemap-generator.py data.csv "My Treemap"

Expects CSV with columns named 'Item' and 'Total'.

Specify custom columns

uv run treemap-generator.py sales.csv "Sales Data" --label-col Product --value-col Revenue

Add currency prefix

uv run treemap-generator.py budget.csv "Budget Overview" -l Category -v Amount -p "$"

Custom output filename

uv run treemap-generator.py data.csv "Q4 Results" -o q4_results

This creates q4_results.png and q4_results.svg.

Complete example with all options

uv run treemap-generator.py expenses.csv "Department Expenses" \
  --label-col Department \
  --value-col Total_Spent \
  --output dept_expenses \
  --prefix "$"

CSV File Format

Your CSV file should have at least two columns:

  • One for labels (e.g., names, categories)
  • One for numeric values

Example CSV:

Item,Total
Software,125000
Hardware,85000
Services,45000
Training,15000

Or with custom column names:

Category,Amount
Marketing,250000
Engineering,500000
Sales,180000

Output

The script generates two files:

  • <output_prefix>.png - Raster image
  • <output_prefix>.svg - Vector image

Treemap tiles display:

  • Bold label (top)
  • Formatted value with optional prefix (bottom)

Values are formatted with comma separators exactly as provided in the CSV (e.g., "$125,000" or "$125,000.50").

Notes

  • If a specified column is not found, the script will display available columns and exit
  • Without the --prefix option, values display without any prefix symbol
  • The prefix can be any string (e.g., "$", "€", "£", "¥")

Error Handling

The script validates:

  • CSV file exists
  • Specified columns exist in the CSV
  • Provides helpful error messages with available column names

Help

View all options:

uv run treemap-generator.py --help

About

Generate Treemap graphs of CSV data

Topics

Resources

License

Stars

Watchers

Forks

Languages