{"id":7751,"library":"squarify","title":"Squarify","description":"Squarify is a pure Python library that implements the squarify treemap layout algorithm. It's commonly used for data visualization, specifically to create treemaps, often in conjunction with Matplotlib. The library is currently at version 0.4.4 and appears to have an active release cadence, with the latest version released in July 2024.","status":"active","version":"0.4.4","language":"en","source_language":"en","source_url":"https://github.com/laserson/squarify","tags":["data visualization","treemap","matplotlib"],"install":[{"cmd":"pip install squarify","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for the `squarify.plot` helper function to render treemaps.","package":"matplotlib","optional":true}],"imports":[{"note":"The common pattern is to import the entire module as 'squarify', then call functions like `squarify.plot()` or `squarify.squarify()`.","wrong":"from squarify import squarify","symbol":"squarify","correct":"import squarify"},{"note":"Required when using the `squarify.plot` function for visualization.","symbol":"pyplot","correct":"import matplotlib.pyplot as plt"}],"quickstart":{"code":"import squarify\nimport matplotlib.pyplot as plt\n\nsizes = [500, 433, 78, 25, 25, 7]\nlabels = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E', 'Category F']\ncolors = ['#4C72B0', '#55A868', '#C44E52', '#8172B2', '#CCB974', '#64B5CD']\n\n# Sort sizes in descending order as required by squarify\nsizes.sort(reverse=True)\n\nplt.figure(figsize=(10, 6))\nsquarify.plot(sizes=sizes, label=labels, color=colors, alpha=0.8)\nplt.title('Treemap Visualization with Squarify')\nplt.axis('off') # Removes axes to show only the treemap\nplt.show()","lang":"python","description":"This quickstart demonstrates how to create a basic treemap using `squarify.plot()`. It shows how to define sizes and labels, sort the sizes (a requirement for squarify), and render the treemap using Matplotlib."},"warnings":[{"fix":"Always sort your `sizes` list: `sizes.sort(reverse=True)`.","message":"Input `sizes` list must be positive values and sorted in descending order before passing to `squarify.squarify` or `squarify.plot`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `normalized_sizes = squarify.normalize_sizes(original_sizes, width, height)` before calling `squarify.squarify(normalized_sizes, ...)`.","message":"For `squarify.squarify` (the layout function), the sum of `sizes` should equal the total area (`dx * dy`). Use `squarify.normalize_sizes(values, dx, dy)` to prepare your values if they don't already match the target area.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of this characteristic; for applications requiring strict hierarchy visibility or layout stability for animations, consider alternative treemap implementations.","message":"The `squarify` algorithm prioritizes aspect ratios close to 1. This can sometimes lead to less clarity in hierarchical structures or instability in layout with small data changes compared to other treemap algorithms.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Rename any local file named `squarify.py` or ensure `import matplotlib.pyplot as plt` is present if you intend to use `squarify.plot`.","cause":"This typically occurs if a local file named `squarify.py` exists in your working directory, shadowing the installed `squarify` library. Another cause can be not importing `matplotlib.pyplot` if `squarify.plot` is being used.","error":"AttributeError: module 'squarify' has no attribute 'plot'"},{"fix":"Install the package using `pip install squarify`. If using a virtual environment or Jupyter/IDE, ensure the correct Python interpreter path is selected.","cause":"The `squarify` package is not installed in the Python environment you are currently using, or there's a mismatch between where it's installed and where your interpreter is looking.","error":"ModuleNotFoundError: No module named 'squarify'"},{"fix":"Manually adjust font size using `text_kwargs={'fontsize': N}` or consider adding custom logic for label placement/wrapping for smaller squares. For highly dynamic or dense treemaps, some labels may need to be omitted.","cause":"By default, `squarify.plot` may not automatically adjust label font sizes or positions for very small rectangles, leading to visual clutter.","error":"Labels for small treemap squares overlap or go out of bounds, making them unreadable."}]}