$ show has2k1/plotnine
plotnine is a mature Python library that implements the Grammar of Graphics, closely modeled after R's ggplot2. It provides a declarative, layered API for building statistical visualizations: users compose plots by adding geometric objects (geoms), statistical transformations (stats), scales, coordinate systems, facets, themes, and guides using the `+` operator. The library renders all output through matplotlib, supporting a wide range of plot types (scatter, bar, line, histogram, density, boxplot, violin, contour, maps, etc.) with extensive customization via themes and scales. It includes built-in example datasets, animation support, plot composition (arranging multiple plots), and integration with Jupyter/Quarto. The codebase is well-structured with a clear separation of concerns (geoms, stats, scales, coords, facets, themes, guides, composition) and employs several reusable design patterns such as a registry metaclass for automatic subclass registration, dataclass-based scale definitions, a hierarchical themeable system, and a docstring generation decorator. External dependencies include matplotlib, pandas, numpy, scipy, statsmodels, mizani, and contourpy, with optional support for adjustText, geopandas, and skmisc. The project is actively maintained with weekly CI tests against bleeding-edge dependencies and a comprehensive test suite using baseline image comparison.
The LLM identified 4 files worth highlighting for reuse:
Defines the `Register` metaclass (subclass of ABCMeta) that automatically registers all non-abstract subclasses into a class-level `_registry` dict, and the `Registry` class (a weak-value dictionary) for name-based lookup. This pattern is used by geoms, stats, scales, positions, and guides to enable string-based instantiation (e.g., `geom='point'`). It is a clean, reusable implementation of the registry pattern that can be extracted for any plugin or factory system.
Implements the entire themeable element hierarchy for plotnine, with ~180 concrete themeable classes (e.g., `axis_text_x_bottom`, `panel_grid_major_y`, `plot_title`) organized through reverse inheritance (parent classes compose children). The `Themeables` container handles merging, blanking, and property fallback via MRO. This is a sophisticated, non-obvious design for fine-grained plot styling that could be adapted for any styling system requiring hierarchical property resolution.
Provides the `@document` decorator (and specialized variants `document_geom`, `document_stat`) that auto-generates docstrings for geom, stat, and other classes from class attributes like `DEFAULT_AES`, `REQUIRED_AES`, `DEFAULT_PARAMS`, and `_aesthetics`. This pattern reduces documentation duplication and ensures docs stay in sync with code. It is a reusable metaprogramming technique applicable to any library with many similar classes.
Contains reusable binning utilities: `breaks_from_binwidth`, `assign_bins`, `fuzzybreaks`, and `bin_centers`. These functions compute bin boundaries and assign data to bins, supporting various binning strategies (Freedman-Diaconis, histodot, etc.). They are self-contained, well-documented, and could be extracted for use in any statistical or data-processing pipeline that requires binning.