A Grammar of Graphics for Python

plotnine is an implementation of a grammar of graphics in Python based on ggplot2. The grammar allows you to compose plots by explicitly mapping variables in a dataframe to the visual objects that make up the plot.

Plotting with a grammar of graphics is powerful. Custom (and otherwise complex) plots are easy to think about and build incremently, while the simple plots remain simple to create.

Example

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap
from plotnine.data import mtcars

(
    ggplot(mtcars, aes("wt", "mpg", color="factor(gear)"))
    + geom_point()
    + stat_smooth(method="lm")
    + facet_wrap("gear")
)

Installation

plotnine can be installed in a couple of ways, depending on the purpose.

Installing the Official Release

For a normal user, it is recommended to install the official release. You can choose one of these two commands:

# Using pip
$ pip install plotnine         # 1. should be sufficient for most
$ pip install 'plotnine[all]'  # 2. includes extra/optional packages

# Or using conda
$ conda install -c conda-forge plotnine

The second pip command also installs packages that are required for some specific functionality that may not be frequently used. Those packages are:

Installing the Development Version

To do any development you have to clone the Plotnine source repository and install the package in development mode. These commands do all of that:

$ git clone https://github.com/has2k1/plotnine.git
$ cd plotnine
$ pip install -e .

If you only want to use the latest development sources and do not care about having a cloned repository, e.g. if a bug you care about has been fixed but an official release has not come out yet, then use this command:

$ pip install git+https://github.com/has2k1/plotnine.git