A Grammar of Graphics for Python providing a declarative approach to data visualization similar to R's ggplot2
—
Aesthetic mappings connect data variables to visual properties like position, color, size, and shape. They form the foundation of the Grammar of Graphics by defining how data is represented visually. Plotnine provides flexible mapping functions that support both direct data mapping and computed values from statistical transformations and scales.
The aes function creates mappings between data variables and visual aesthetics, defining how data values are converted to visual properties.
def aes(x=None, y=None, **kwargs):
"""
Create aesthetic mappings between data variables and visual properties.
Common aesthetic parameters:
- x, y: str, column names for position aesthetics
- color/colour: str, column name for color aesthetic
- fill: str, column name for fill color aesthetic
- size: str, column name for size aesthetic
- alpha: str, column name for transparency aesthetic
- shape: str, column name for point shape aesthetic
- linetype: str, column name for line type aesthetic
- stroke: str, column name for stroke width aesthetic
- group: str, column name for grouping aesthetic
Returns:
aes object (extends dict) containing aesthetic mappings
"""Usage example:
from plotnine import ggplot, aes, geom_point
import pandas as pd
data = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [2, 5, 3, 8, 7],
'color_var': ['A', 'B', 'A', 'B', 'A'],
'size_var': [1, 3, 2, 4, 2]
})
# Basic position mapping
ggplot(data, aes(x='x', y='y'))
# Multiple aesthetic mappings
ggplot(data, aes(x='x', y='y', color='color_var', size='size_var'))Functions for mapping to values computed during the plotting process, allowing access to statistical transformations and scale mappings.
def after_stat(x):
"""
Reference values computed by statistical transformations.
Parameters:
- x: str, name of computed statistic
Common computed statistics:
- count: number of observations
- density: density values
- prop: proportions
- level: contour levels
- quantile: quantile values
Returns:
after_stat object for use in aesthetic mappings
"""
def after_scale(x):
"""
Reference values after scale transformation.
Parameters:
- x: str, name of aesthetic after scaling
Returns:
after_scale object for use in aesthetic mappings
"""
def stage(start=None, after_stat=None, after_scale=None):
"""
Control evaluation timing of aesthetic mappings.
Parameters:
- start: expression evaluated on original data
- after_stat: expression evaluated after statistical transformation
- after_scale: expression evaluated after scale transformation
Returns:
stage object that evaluates expressions at appropriate times
"""Usage examples:
# Map fill to computed count in histogram
ggplot(data, aes(x='value')) + geom_histogram(aes(fill=after_stat('count')))
# Map color to density values
ggplot(data, aes(x='x', y='y')) + geom_density_2d(aes(color=after_stat('level')))
# Access scaled values
ggplot(data, aes(x='x', y='y')) + geom_point(aes(size=after_scale('x')))
# Stage different computations
ggplot(data, aes(x='value')) + geom_histogram(
aes(fill=stage(start='group', after_stat='count'))
)# Primary position
aes(x='column_name', y='column_name')
# Secondary position (for range geoms)
aes(xmin='min_col', xmax='max_col', ymin='min_col', ymax='max_col')
# Positioning for specific geoms
aes(xend='end_x', yend='end_y') # For segments
aes(slope='slope_col', intercept='intercept_col') # For ablines# Color (for points, lines, text)
aes(color='group_column')
aes(colour='group_column') # British spelling also supported
# Fill (for areas, bars, polygons)
aes(fill='category_column')
# Both color and fill
aes(color='group', fill='category')# Size (for points and lines)
aes(size='numeric_column')
# Shape (for points)
aes(shape='category_column')
# Stroke width
aes(stroke='width_column')# Alpha (transparency)
aes(alpha='transparency_column')
# Line type
aes(linetype='line_category')
# Grouping (important for line plots)
aes(group='grouping_column')# Global aesthetics (apply to all layers)
ggplot(data, aes(x='x', y='y', color='group')) + \
geom_point() + \
geom_line()
# Local aesthetics (apply to specific layer)
ggplot(data, aes(x='x', y='y')) + \
geom_point(aes(color='group')) + \
geom_line(color='blue') # Fixed value, not mapped# Mapping variable to aesthetic (inside aes())
ggplot(data, aes(x='x', y='y')) + geom_point(aes(color='group'))
# Setting fixed value (outside aes())
ggplot(data, aes(x='x', y='y')) + geom_point(color='blue', size=3)
# Mixed approach
ggplot(data, aes(x='x', y='y')) + geom_point(aes(color='group'), size=3)# Histogram with fill mapped to computed count
ggplot(data, aes(x='value')) + \
geom_histogram(aes(fill=after_stat('count')), bins=20)
# Bar plot with proportion instead of count
ggplot(data, aes(x='category')) + \
geom_bar(aes(y=after_stat('prop')))
# Density plot with color mapped to computed density
ggplot(data, aes(x='value')) + \
geom_density(aes(color=after_stat('density')))# Transform data in aesthetic mapping
ggplot(data, aes(x='x', y='y**2')) # Square the y values
# Conditional mapping
ggplot(data, aes(x='x', y='y', color='value > 5'))
# String concatenation
ggplot(data, aes(x='x', y='y', label='name + ": " + str(value)'))Install with Tessl CLI
npx tessl i tessl/pypi-plotnine