Plotnine
Plotnine is a Python package for data visualization, implementing a grammar of graphics inspired by R's ggplot2. It allows users to compose plots by explicitly mapping data variables to visual aesthetics, making it powerful for creating custom and complex visualizations incrementally. The library is actively maintained, with frequent releases. The current stable version is 0.15.3.
Warnings
- breaking Calling `print(ggplot_obj)` no longer renders the plot; it now returns the display size in pixels. This change was fully implemented in v0.14.0 after being deprecated in v0.13.0.
- breaking Plotnine v0.14.0 and newer require Python 3.10 or later.
- deprecated Several themeables related to axis tick padding (e.g., `axis_ticks_pad`, `axis_ticks_pad_minor_x`) have been deprecated in v0.15.0.
- gotcha When constructing multi-line plots using the `+` operator, forgetting to wrap the entire expression in parentheses can lead to `SyntaxError` or incorrect parsing.
- gotcha When mapping aesthetics, `aes()` expects column names as strings for mapping data to visual properties. Manually setting a constant aesthetic (e.g., making all points blue) should be done outside `aes()` as a direct argument to the geom.
- gotcha Plotnine's dependency on `mizani` (for scales) can sometimes lead to version conflicts with other packages, particularly in older installations.
Install
-
pip install plotnine -
pip install 'plotnine[extra]'
Imports
- ggplot, aes, geom_point
from plotnine import ggplot, aes, geom_point
- *
from plotnine import *
Quickstart
import pandas as pd
from plotnine import ggplot, aes, geom_point
from plotnine.data import penguins
# Create a basic scatter plot using the built-in penguins dataset
plot = (ggplot(penguins, aes(x='bill_length_mm', y='bill_depth_mm', color='species')) +
geom_point())
# To display the plot (e.g., in a script or non-notebook environment)
# plot.show()
# In a Jupyter notebook or interactive environment, simply having the
# plot object as the last line will display it.
plot