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.
pip install plotnineVerified import paths — ran on the pinned version, not inferred.
This example creates a scatter plot of bill length vs. bill depth, colored by penguin species, using the built-in `penguins` dataset. It demonstrates the fundamental `ggplot`, `aes`, and `geom_point` components of plotnine.
Use `ggplot_obj.show()` to explicitly display the plot, especially in scripts or non-notebook environments where the plot object isn't implicitly rendered.
Ensure your Python environment is version 3.10 or higher.
Use the `margin` parameter of `element_text` with `axis_text`, `axis_text_x`, or `axis_text_y` to control spacing between axis text and ticks.
Always enclose multi-line plot constructions within parentheses (e.g., `(ggplot(...) + geom_point() + ...)`).
For data mapping: `aes(color='column_name')`. For manual setting: `geom_point(color='blue')`. Do NOT use `aes(color='blue')` to set a constant color.
Always install `plotnine` and its dependencies in a fresh virtual environment. If issues arise, check the `mizani` version specified in `plotnine`'s `pyproject.toml` on GitHub and consider pinning `mizani` to a compatible version if necessary (e.g., `pip install mizani==x.y.z`).
Ensure all mandatory aesthetics for the chosen `geom` are mapped from your data within `aes()` (e.g., `ggplot(df, aes(x='column_name_x', y='column_name_y')) + geom_point()`).
Verify that the column name specified in `aes()` exactly matches an existing column in your DataFrame, including case sensitivity, or create the column if it's missing.
Downgrade `matplotlib` to a compatible version (e.g., `pip install 'matplotlib<3.6'`) or upgrade `plotnine` to its latest version that supports newer `matplotlib` releases (`pip install --upgrade plotnine`).
Move fixed aesthetic assignments outside of `aes()`, directly as arguments to the `geom_` function (e.g., `geom_point(color='blue')` instead of `geom_point(aes(color='blue'))`).