CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-plotnine

A Grammar of Graphics for Python providing a declarative approach to data visualization similar to R's ggplot2

Pending
Overview
Eval results
Files

core-plotting.mddocs/

Core Plot Construction

The foundation for all plotnine visualizations, providing the main ggplot class for building layered graphics, quick plotting functions for simple visualizations, and save functionality for exporting plots to various formats.

Capabilities

Main Plot Class

The ggplot class is the core of plotnine, implementing the Grammar of Graphics approach where plots are built by adding layers with the + operator.

class ggplot:
    """
    Main plotting class that serves as the foundation for all plots.
    
    Parameters:
    - data: pandas.DataFrame, the data to be plotted
    - mapping: aes object, default aesthetic mappings for the plot
    
    Methods:
    - save(): Save plot to file
    - show(): Display plot  
    - draw(): Force drawing of the plot
    """
    def __init__(self, data=None, mapping=None): ...
    
    def save(self, filename, width=6.4, height=4.8, dpi=100, format=None, 
             units='in', limitsize=True, verbose=True, **kwargs): ...
    
    def show(self): ...
    
    def draw(self, return_ggplot=False): ...

Usage example:

import pandas as pd
from plotnine import ggplot, aes, geom_point

data = pd.DataFrame({'x': [1, 2, 3], 'y': [1, 4, 9]})
plot = ggplot(data, aes(x='x', y='y')) + geom_point()
print(plot)  # Display the plot

Quick Plot Function

The qplot function provides a convenient interface for creating simple plots quickly, similar to R's qplot.

def qplot(x=None, y=None, data=None, facets="", margins=False, geom="auto", 
          xlim=None, ylim=None, log=None, main=None, xlab=None, ylab=None, 
          asp=None, **kwargs):
    """
    Quick plot function for simple visualizations.
    
    Parameters:
    - x, y: str or array-like, variables to plot
    - data: pandas.DataFrame, data source
    - facets: str, faceting formula
    - margins: bool, whether to display marginal facets
    - geom: str or list, geometric object(s) to use
    - xlim, ylim: tuple, axis limits
    - log: str, which axes to log transform ('x', 'y', 'xy')
    - main: str, plot title
    - xlab, ylab: str, axis labels
    - asp: float, aspect ratio
    - **kwargs: additional aesthetic mappings and parameters
    
    Returns:
    ggplot object
    """

Usage example:

import pandas as pd
from plotnine import qplot

data = pd.DataFrame({'x': [1, 2, 3], 'y': [1, 4, 9]})
plot = qplot(x='x', y='y', data=data, geom='point')

Save Functions

Functions for saving plots to various file formats and handling multiple plots.

def ggsave(filename, plot=None, width=6.4, height=4.8, dpi=100, format=None,
           units='in', limitsize=True, verbose=True, **kwargs):
    """
    Save plot to file (alias for ggplot.save method).
    
    Parameters:
    - filename: str, output filename
    - plot: ggplot object, plot to save (uses last plot if None)
    - width, height: float, dimensions in specified units
    - dpi: int, resolution in dots per inch
    - format: str, file format (inferred from filename if None)
    - units: str, units for width/height ('in', 'cm', 'mm')
    - limitsize: bool, whether to limit size to reasonable values
    - verbose: bool, whether to print save information
    - **kwargs: additional matplotlib savefig parameters
    """

def save_as_pdf_pages(plots, filename=None, **kwargs):
    """
    Save multiple plots to a single PDF file, one plot per page.
    
    Parameters:
    - plots: list of ggplot objects
    - filename: str, output PDF filename
    - **kwargs: additional parameters passed to PdfPages and savefig
    
    Returns:
    PdfPages object if filename is None, otherwise None
    """

Usage examples:

# Save single plot
plot.save('my_plot.png', width=8, height=6, dpi=300)

# Using ggsave
ggsave('my_plot.pdf', plot, width=10, height=8)

# Save multiple plots to PDF
plots = [plot1, plot2, plot3]
save_as_pdf_pages(plots, 'multiple_plots.pdf')

Plot Display and Interaction

# Display plot in Jupyter notebook or interactive environment
plot  # Simply evaluating the plot object displays it

# Force display
print(plot)

# In Jupyter notebooks, plots display automatically
plot  # Shows plot inline

Usage Patterns

Basic Plot Construction

# Start with data and aesthetic mappings
p = ggplot(data, aes(x='column1', y='column2'))

# Add geometric objects
p = p + geom_point()

# Add additional layers
p = p + geom_smooth(method='lm')

# Customize appearance  
p = p + theme_minimal() + labs(title='My Plot')

# Display or save
print(p)
p.save('plot.png')

Chained Construction (Recommended)

plot = (ggplot(data, aes(x='x', y='y')) +
        geom_point(color='blue', size=3) +
        geom_smooth(method='lm', color='red') +
        theme_minimal() +
        labs(title='Scatter Plot with Trend Line',
             x='X Variable', y='Y Variable'))

Quick Plotting for Exploration

# Quick scatter plot
qplot('x', 'y', data=df, geom='point')

# Quick histogram
qplot('x', data=df, geom='histogram', bins=30)

# Quick box plot by group
qplot('group', 'value', data=df, geom='boxplot')

Install with Tessl CLI

npx tessl i tessl/pypi-plotnine

docs

aesthetic-mappings.md

coordinate-systems.md

core-plotting.md

faceting.md

geometric-objects.md

guides-and-legends.md

index.md

labels-and-annotations.md

position-adjustments.md

sample-datasets.md

scales-and-axes.md

statistical-transformations.md

themes-and-styling.md

watermarks.md

tile.json