Skip to main content

Python port of the R ggplot2 package (tracks R ggplot2 4.0.2.9000)

Project description

ggplot2_py ggplot2_py logo

AI-assisted Python port of the R ggplot2 package — Create Elegant Data Visualisations Using the Grammar of Graphics.

Overview

ggplot2_py implements the grammar of graphics in Python, faithfully porting R's ggplot2 using pandas DataFrames as the data container and a Cairo-based rendering backend. It supports 47 geoms, 32 stats, faceting, coordinate systems, themes, guides, and 130+ scales.

Beyond a direct port, ggplot2_py adds Python-exclusive features that extend the Grammar of Graphics with Python-native idioms while preserving full orthogonality of GOG components.

Python-Exclusive Features

These capabilities have no R equivalent and leverage Python-specific language features:

Feature Python mechanism What it enables
Callable aes() expressions First-class functions / lambdas aes(y=lambda d: np.log(d["mpg"])) — inline data transforms without pre-computing columns
after_stat() / after_scale() callables Same after_stat(lambda d: d["count"] / d["count"].sum()) — arbitrary expressions at each pipeline stage
singledispatch extensibility functools.singledispatch @update_ggplot.register(MyClass) — any Python class can be added to a plot with +
Build hooks Dict-keyed callback lists plot.add_build_hook("after", BuildStage.COMPUTE_STAT, fn) — intercept data at any of 16 named pipeline stages
Auto-registration __init_subclass__ class GeomStar(Geom): ... auto-registers; no manual wiring needed
Protocol contracts typing.Protocol isinstance(my_geom, GeomProtocol) — structural type checking for extensions
Scoped defaults contextvars.ContextVar with ggplot_defaults(theme=theme_minimal()): ... — thread-safe scoped defaults

Installation

# From PyPI
pip install ggplot2-python

For a local development:

git clone https://github.com/Bio-Babel/ggplot2-python.git
cd ggplot2_py
pip install -e ".[dev]"

Quick Start

from ggplot2_py import *
from ggplot2_py.datasets import mpg

(ggplot(mpg, aes(x="displ", y="hwy", colour="class"))
 + geom_point()
 + geom_smooth(method="lm")
 + facet_wrap("drv")
 + theme_minimal()
 + labs(title="Engine Displacement vs Highway MPG"))

Callable expressions in aes (Python-exclusive)

import numpy as np
from ggplot2_py import ggplot, aes, geom_point, geom_histogram, after_stat

# Inline data transform — no need to pre-compute a column
ggplot(mpg, aes(x=lambda d: np.log(d["displ"]), y="hwy")) + geom_point()

# Normalised histogram — callable in after_stat
(ggplot(mpg, aes(x="hwy"))
 + geom_histogram(aes(y=after_stat(lambda d: d["count"] / d["count"].sum())), bins=15))

Extending with custom types (Python-exclusive)

from ggplot2_py import update_ggplot

class Watermark:
    def __init__(self, text): self.text = text

@update_ggplot.register(Watermark)
def _add_watermark(obj, plot, object_name=""):
    plot.labels["caption"] = f"[{obj.text}]"
    return plot

# Now use with +
ggplot(mpg, aes("displ", "hwy")) + geom_point() + Watermark("DRAFT")

Scoped defaults (Python-exclusive)

from ggplot2_py import ggplot_defaults

with ggplot_defaults(theme=theme_minimal()):
    p1 = ggplot(df, aes("x", "y")) + geom_point()   # theme_minimal applied
    p2 = ggplot(df, aes("x", "y")) + geom_bar()     # theme_minimal applied
# Outside: no defaults

Tutorials

User Tutorials

  • Getting Started — core concepts: data, aes, geoms, stats, scales, facets, coords, themes
  • Geom Gallery — boxplot, violin, density, tile, hex and combinations
  • Labels & Facets — axis titles, plot title/subtitle/caption, facet strip labels
  • Aesthetic Specs — colour, fill, alpha, linetype, shape, size, colour scales
  • Extending ggplot2 — custom stats, geoms, themes via ggproto

Python-Exclusive Feature Tutorials

Developer Guide

  • Developer Guide: Extending ggplot2_py — comprehensive guide covering ggproto system, custom Stat/Geom creation, Protocol contracts, singledispatch, hooks, auto-registration, context manager, and packaging

Extension Architecture

ggplot2_py is designed as an extensible platform. The following table summarises all extension points:

Extension point Mechanism How to use
Custom Stat Subclass Stat Override compute_group() — auto-registered via __init_subclass__
Custom Geom Subclass Geom Override draw_panel() — auto-registered
Custom Scale Subclass ScaleContinuous / ScaleDiscrete Implement train(), map(), get_breaks()
Custom Coord Subclass Coord Override transform(), setup_panel_params()
Custom Facet Subclass Facet Override compute_layout(), map_data()
Custom Position Subclass Position Override compute_layer() — auto-registered
Custom + types @update_ggplot.register(MyClass) Register any Python class for the + operator
Custom plot types @ggplot_build.register(MyPlot) Override the entire build pipeline
Build hooks plot.add_build_hook(timing, stage, fn) Intercept data at any pipeline stage
Protocol validation isinstance(obj, GeomProtocol) Verify structural conformance
Scoped defaults with ggplot_defaults(theme=...): Thread-safe scoped defaults

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

ggplot2_python-4.0.2.9000.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

ggplot2_python-4.0.2.9000-py3-none-any.whl (1.2 MB view details)

Uploaded Python 3

File details

Details for the file ggplot2_python-4.0.2.9000.tar.gz.

File metadata

  • Download URL: ggplot2_python-4.0.2.9000.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for ggplot2_python-4.0.2.9000.tar.gz
Algorithm Hash digest
SHA256 507dc21653baf5e7181a8fe9079b426ae65d55e80675d34ef224a31161bdd69b
MD5 1070eb24c4d14f8a9b8746bc2542e58d
BLAKE2b-256 f1de338ba2c900334f1df8da2790eafbc30a866866333bb217e1c978706ab090

See more details on using hashes here.

File details

Details for the file ggplot2_python-4.0.2.9000-py3-none-any.whl.

File metadata

File hashes

Hashes for ggplot2_python-4.0.2.9000-py3-none-any.whl
Algorithm Hash digest
SHA256 50e10f7311cf529abe25ae8e30f22027d0ac46680a1f115f28a38c9ea68c14ea
MD5 6b6972d6a3998c8cb0cbd2ad0f2de955
BLAKE2b-256 7c0c22f89caefd344d2fecb362bec6620ed04a4675bf13044f55aca407f21707

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